No announcement yet.

How can I put PHP in my HTML files?

  • Time
  • Show
Clear All
new posts

  • How can I put PHP in my HTML files?

    Have you ever wished you could execute PHP scripts within your HTML files? If you have worked with PHP at all in the past, I am sure there have been times when coding PHP right into your .html files would have been very convenient, not to mention a time saver. This tutorial will show you how to parse your PHP scripts into .html files instead of having to name your file with a .php extension.

    ***Please note this only works on an Apache server.***

    Before you start thinking about how hard this will be, or how much time it will take you, let me ease your burden. It does not involve any coding at all. The script you will use is very short, and has been provided for you within this tutorial. So all you will have to do is use the copy and paste feature of your browser. The entire process should not take you more then five (5) minutes to go through.

    Modifying the .htaccess file:

    :1: This process involves modifying your .htaccess file, which resides within your root directory. For most hosts the path is as follows:

    path: /home/username/public_html/

    Find the file and open it. If you do not have and .htaccess file in your root directory, then you will need to create one.

    See step 2 for creating an .htaccess file.

    :2: If you have an .htaccess file already, then you can skip to step 3. To create an .htaccess file open Notepad or a similar text program, or you can even use your favorite HTML editor if desired. Create a new blank document and save it as .htaccess.

    Don't worry if it adds an extension to the filename, this can be corrected when you upload the file to your hosting account.

    Make sure to keep the file open, as we will be pasting our code into this file. (This step will add a little more time to the process.)

    :3: Now you will want to copy the code below and add (paste) it to your .htaccess file.

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

    :4: After you have pasted the above code into your .htaccess file, you will need to save it. If you are modifying your document via your control panel or FTP program, then saving it is all you will need to do to activate the new file. If you used step 2 to create your .htaccess file from scratch, then you will need to upload your file to your hosting account. Once uploaded use your control panel or FTP program to remove the filename extension if it was added to the end when creating the document. The file should simply be called ".htaccess" for it to work right.

    :Success: You can now execute PHP scripts in .html files. This means that you will never have to name a file with the .php extension again, unless you want to. Congratulations! Just to be safe though let's test it out.

    Testing 1, 2, 3:

    To make sure your work was not in vain, let's test it out.

    :1: Create an HTML file and name it test.html.

    :2: Now copy and paste the following into the file.

              <title>Testing your work.</title>
    <?php echo "It worked! Thanks CWEB for helping me parse PHP 
    in my HTML files."; ?>
    :3: Save the file and upload it to your hosting account. Now view the page in your browser. If you see the "It worked!." text then it is working. If you don't see the text then ask for help in our forums. We will be happy to get you pointed in the right direction.

    Courtesy of BJacobs
    Last edited by AndrewT; 12-21-2007, 10:29 AM.
    Dan Blomberg