How to call PHP script within HTML files

Catagories: CSS / Webdesign, GUI, Operating system, PHP, Programming, Windows, cPanel / February 26th, 2008
When I ask you to listen
KafeInfo.com - bookmarking sosial Anda

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 :)

bookmark
  • StumbleUpon
  • Digg
  • del.icio.us
  • YahooMyWeb
  • Technorati
  • Netvouz
  • description
  • ThisNext
  • MisterWong
  • Wists
  • Netscape
  • Haohao
When I ask you to listen
KafeInfo.com - bookmarking sosial Anda

8 Responses to 'How to call PHP script within HTML files'

Subscribe to comments with RSS or TrackBack to 'How to call PHP script within HTML files'.

  1. Terry said, on February 26th, 2008 at 18:13

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

  2. Bruce Lee said, on February 28th, 2008 at 2:45

    This is really great to know

  3. dedi said, on March 11th, 2008 at 15:52

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

  4. get inbound links said, on May 27th, 2008 at 6:44

    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 said, on June 10th, 2008 at 22:16

    how to add in Blogspot Blog

  6. marcellinosantoso said, on June 15th, 2008 at 3:51

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

  7. Lara said, on September 5th, 2008 at 12:09

    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 said, on December 6th, 2008 at 13:42

    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.

Leave a Reply