Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • Google+
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Web Servers ›
Running PHP code in HTML files

Running PHP code in HTML files

August 21, 2010 Web Servers Jesin A 3 Comments

PHP can be useful when used in conjunction with html. Even if you don’t plan create full blown dynamic pages certain functions like server side includes can help you repeat the code less for commonly used things like menus and advertisements. But it can be irritating to end the file with a .php extension just because you’ve added a snippet of PHP code. Also some people say that pages with a .html extension rank more in search engines when compared to .php. I don’t know how far its true and I’ll leave it in your hands to experiment.

In this article I’ll focus on the things needed to be done for html to be parsed by the server. (More about this word later)

Step 1: Create/Edit the .htaccess or httpd.conf file

In case you’re running your own web server or having a dedicated server/VPS hosting your website the you’ll definitely have access to the httpd.conf file. Open the file using a text editor, go to the end of the file and add the following line.

AddType application/x-httpd-php .html

Save the file httpd.conf and restart Apache server for the changes to take place.

(or)

If you’re hosting your website on a shared server you can do the same thing using the htaccess file. Create a file name .htaccess (not the dot at the beginning) if it doesn’t already exist or edit it if it exists. Add the same line shown above and save the file. If you follow this step you don’t need to restart Apache Server. If you want to add .htm extension too you can do so in the following manner.

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

Step 2: Testing the New Configuration

Now its time to test if the changes made actually work. Create any file with a .html e.g. phpcode.html. Write the following PHP code to that file.

<?php
print “<h1>Your IP address is ” . $_SERVER[‘REMOTE_ADDR’] . “</h1>”;
?>

Save the file and access it via your browser by typing http://yourwebsitename.com/phpcode.html. If you’ve done everything right you should see the sentence “Your IP address 192.168.1.1” or whatever is you IP address in heading 1 form.

If you get a 500 Internal Server Error check the syntax in the htaccess file. Another possible error is you see the PHP code displayed in the browser. In this case if you’ve used the httpd.conf method you should restart Apache Service and make sure created a test file with the same extension as entered at the end of AddType i.e. if you enter the .html extension and create a test file with .htm extension it won’t work. If this is the case you better add both the extensions in AddType.

What is parsing?

Earlier I mention the word parsing. Parsing is the process of analyzing the code in a particular file and executing it the way it should be executed. For example if a web browser sees a <b> tag it renders text in bold format till the it encounters a close </b> tag.

Similarly Apache Web server “parses” the code in each and every file with a .php extension and executes the PHP code. Now by adding the snippet AddType application/x-httpd-php .html we are telling the web server to treat files with a .html extension the same way it would treat a php file. So now the web server will parse .html files also.

Related posts:

php category thumbnailHow to use custom php.ini files on shared hosting Default ThumbnailChange the default Directory Index in Apache Server disable directory listing apache thumbnailDisable Directory Listing in Apache Default ThumbnailUsing the Apache Redirect directive php category thumbnailHow to use PHP to minify HTML output

Tags: addtype, htaccess, php in html

Comments

  1. Ted says

    June 17, 2017 at 9:31 pm

    This is a HORRIBLE idea. I just worked with someone who quoted this post and has been putting PHP in HTML files for years. PLEASE either remove this post or edit it to say that this is not in any way recommended.

    Reply
    • Jesin A says

      June 18, 2017 at 1:31 am

      Hey Ted,

      A Google search for “parse html as php” returns results linking to sites like StackOverflow with similar answers written by people with a very high reputation (like this and this). I’m not saying this is a good idea, but I believe someone using this method over URL rewriting has different priorities when programming.

      Reply
  2. Dany says

    March 14, 2018 at 6:28 pm

    This post is a great help for someone how wants the php code parsed in the HTML file.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.