online showcase of my works, blog posts, tweets, and everything else

How to call PHP script within HTML files

Will code HTML for food - God bless you!

First off : Why the heck should you have/use PHP?

PHP is server side scripting. That means it is parsed on the server before your page loads in the browser. To the visitor and to the search engines PHP scripts are not noticeable. When PHP is combined with RSS feeds, links, blog entries, dynamic portfolio, it is a pretty powerful concept. With PHP you can add other people’s content to your web pages and the search engines will see it and think it is part of your website! (Think of the SEO-thingies). That PHP-parsed content when placed on your web page will essentially become a part of your web site and bring you more traffic. That is why most web developers recommend that you add the content from external sites to your website with PHP instead of Javascript.

Which comes first? Chicken or egg?

The characteristic of HTML that it will only parse / call the Javascript from within the HTML-parsing routine. (Okay, we both know it can call whatever files, but for the sake of this post, let’s pretend it’s that way, okay :D ). PHP, ASP, JSP, whatever, can call/use HTML tags/files, and not the other way around.

Yes, I know that you can simply “rewrite” everything into php and enclose the real php in <?php … ?>-tag.
But how should you do it if you insist that the file’s extension is still HTML and still can parse the PHP scripts inside? There are some ways:

  • Option I
    • a: .htaccess for HTM/HTML, IIS
    • b: the same as Part I, only using the cPanel instead the hard way
  • Option II: SSI Include

I. .htaccess (using notepad and ftp)

PHP scripts do not automatically work on html pages. If you have an HTML or (.HTM) based site, in order for PHP scripts to run on your .htm or .html pages you have a 2 options –the following is the option A: In order for a .html or .htm site to parse a PHP script on it’s pages, you need to have some instructions in your website’s .htaccess file. (For this step you will need access to the .htaccess file on your server.)

  1. Edit your .htaccess with AddType application/x-httpd-php .htm .html
    Then Add the php code directly to your html pages
  2. Then edit your .htaccess with either .htm server parsed .html server parsed OR Addhandler application/x-httpd-php .html .php
    Then follow the directions below to use the php code with SSI Includes.

    **NOTE 1:For Windows or IIS servers
    **
    You do not have an .htaccess file. Windows IIS servers also cannot run php on html pages. Your only option for windows IIS servers is to use .shtml or .php pages with the php option from the reader. Or add Javascript to your html pages. For .shtml pages check your setup options and make sure the option for SSI includes is checked, then follow the directions below for creating the SSI include and file.**NOTE 2 : Explicit version number**
    dedi_toblerone
    was kind enough to told me that you need to explicitly mention the PHP version.
    So the code will look like this:
    .htaccess: (the numbers indicates line numbers, no need to include it)
    1: application/x-httpd-php .htm .html
    2: .htm server parsed
    3: .html server parsed
    OR
    3: Addhandler application/x-httpd-php5 .html .php
    OR

    3: AddType application/x-httpd-php5 .html .php

I. .htaccess (using cPanel) – Option B

In order for a .html or .htm site to parse a PHP script on it’s pages, you need to have some instructions in your website’s .htaccess file.

You can either manually modify the .htaccess file on your server (see option I-A), or if you have Cpanel, this can very easily be done using the ‘Apache Handler’ Function.

1. Click on the Apache handlers icon on cPanel Using the ‘Apache Handler’ Function. (Click on the pictures for blow up)Look for the ‘Apache Handler’ Icon in Cpanel and click on it.
2. After you click on the Apache Handlers Icon you will see a screen similar to this one below After you click on the Apache Handlers Icon you will see a screen similar to this one
3. In the example above there are no instructions for .htm and .html pages.

In the example aside there are no instructions for .htm and .html pages. If your Apache form looks like the aside then proceed as directed below.

If there are instructions already listed under “User Defined Handlers” for .html and .htm extensions then you do not need to modify anything at all.

Next we need to add the instructions …

.htm server parsed

.html server parsed

Type .htm server parsed in as directed in the picture, then click on the “Add” button;

4. You will be instructed that your definitions were assigned to the extensions specified on the next screen. You will be instructed that your definitions were assigned to the extensions specified on the next screen.
5. Click on Go Back and you will see the definition you just added defined under Click on Go Back and you will see the definition you just added defined under “User Defined Handlers” as demonstrated in the picture.
6. Repeat the steps above with the extension .html. Click on Add Repeat the steps above with the extension .html, and click on Add
7. Done You’re done :)

II. IIS Includes

SSI includes are short commands that reference another file on your server for inclusion on the web page where it is called from. Meaning you can put any information in an external file and remotely call it into any other page on your server with an SSI command. This is especially useful for making the task of editing large websites easier. When the file that is being included is changed, all of the pages that reference that file will change at the same time, without you having to change them all one by one.

SSI is also useful for parsing PHP scripts into .html or .htm webpages, since PHP by itself does not work on html sites. PHP code works on pages that have a .php extension. But instead of changing all of your webpages from .html or .htm to .php extensions, you can use SSI on your html pages.

The only thing that you need to do is include the following single line into the part where you want the PHP to be called:

<!–#include virtual=”/IWantToCallThisFile.php” –>

Closing words

This post is an excerpt from numerous websites and resources. And yes of course, there are still numerous ways to do this trick. Be so kind to post it on the comments box :)

23 Responses to “How to call PHP script within HTML files”

  1. Terry says:

    I have been looking for a way to do this for months. Thanks. I will definitely give it a try.

  2. Bruce Lee says:

    This is really great to know

  3. dedi says:

    sori lagi sempet mampir kang… hehehe…
    semoga bermanfaat om…
    ^_^

  4. Nice knowledge given. I understood well how to call php from another html.

    get inbound links’s last blog post..How to get inbound links to improve page rank as well as search engine ranking?

  5. siddiq says:

    how to add in Blogspot Blog

  6. @siddiq: please elaborate what do you really want to add in blogspot?

  7. Lara says:

    That was a great article. It was complete. It helped me solve my buddies pblm. I have been trying with the option AddHandler application/x-httpd-php .htm .html but it did not work. But as per your instructions when I added the below two lines it started working. Thank you.
    Addhandler application/x-httpd-php5 .html .php
    AddType application/x-httpd-php5 .html .php

    Lara’s last blog post..Most favourite Bharathiyar Kavithai

  8. Web Designer says:

    To do this in the .htacces file on cPanel apache servers you need to do this:

    (Above your re-write rules)

    #
    # Run PHP scripts in HTML files.
    #

    AddHandler application/x-httpd-php .html
    AddHandler application/x-httpd-php .htm

    Make sure you refresh your cache before testing it.

  9. David says:

    Thanks heaps Lara – that little 5 on the end made all the difference to my issues & it works great here.

  10. pabelmont says:

    Help needed –
    I want to run PHP and HTML stuff on my home-computer (no server for this) under Linux/Ubuntu. Especially (and perhaps solely) to use FORMS with PHP programs. Locally.

    Chief issues:
    [1] accessing URL’s $_GET (or $_POST)
    [2] writing to the general file-system.
    [3]calling HTML from PHP
    [4]calling PHP from HTML
    Any ideas how to do this stuff?

  11. tushar says:

    can we run any php arcade script on blogspot

    if yes

    then please tell how it is possible

  12. @pabelmont:
    -. calling html from php is piece of cake; just link one.
    -. calling php from html: read my post.
    -. accessing GET & POST: read this URL tizag.com
    -. writing to system: change the permission of the file that going to write something to 755 (777 if fails)

  13. @tushar: i don’t think blogspot will allow a script to be run in user’s mode :) sorry. I suggest you mail the blogspot support center. Good luck.

  14. Dan Milner says:

    Lara, you’re a legend!

    Anyone having problems embedding php into html code should refer to Lara’s post on 5th September 2008. I’ve tried loads of sources to sort my problem (including my webspace provider and various experts) but this post sorted it.

    Cheers!

  15. @Dan:

    As you can also see, I also mentioned in my post the steps that Lara wrote. :)

  16. Kris says:

    Thanks so much, I am no expert and this is written so simply!

  17. Argup says:

    My boyfriend wants to just use his iphone GPS app to drive from Alaska to Eugene OR since we are moving. Is this a good idea? Should he get a real GPS for the car? Will it be more reliable?

    —-
    Reply:
    I don’t really know how the iPhone’s GPS performance. I’d suggest you check this out with people at the iphone forums that spread out around the net :-)
    Goodluck with the moving!

  18. Peter says:

    hi Marcellino Santoso,

    You are truly brilliant. I don’t know anything about php and I was trying to pass parameter through the URL to my forms. I had struggled with it for 3 weeks when someone showed me some simple PHP code to do that in my HTML. It worked on his machine but I could not get it to work on my Yahoo! hosted site. I then registered for another host with a new domain name but it did not work there either….. until I found your site.

    Thanks … you are simply brilliant
    Peter

  19. Mr X says:

    Thanx…

  20. visu says:

    hi i have problem with run php with html file………
    in my server .htaccess file not there pls give me how to run php code with html………

Leave a Reply