Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Parsing .html files for php code Internet
I recently wanted to add some PHP code to my web pages, giving the last modification date. The PHP code to do this is:
<?php
putenv("TZ=America/New_York");
echo "Last modified: " .date("F d Y ", getlastmod());
echo "at "  .date("h:i:s A.",getlastmod());
?>
The problem was that all of the pages were .html files, and Apache isn't set up by default to parse .html for PHP code. A search on the web for help found a page [nonpplus.net] that suggested that one add the following to an .htaccess file and place the file in the directory to be affected:
AddType application/x-httpd-php .htm .html.
However, I wanted my whole /Users/user_name/Sites folder to have .html, .htm, and .php parsed for PHP code, so I added
RemoveHandler .htm .html
AddType application/x-httpd-php .php .htm .html
to the bottom of my user_name.conf file in /etc/httpd/users by typing:
sudo pico /etc/httpd/users/username.conf
(Enter your password when prompted). I would assume you could set Apache to parse all .htm, .html, and .php files for PHP by adding some version of the above to httpd.conf in /etc/httpd, but this might have a huge impact on the server's performance. This hint really just collects together two useful pieces of information in one place, but I hope somebody else finds it helpful.
    •    
  • Currently 3.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[12,141 views]  

Parsing .html files for php code | 7 comments | Create New Account
Click here to return to the 'Parsing .html files for php code' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Parsing .html files for php code
Authored by: Beernd on May 30, '03 10:55:52AM

I use Server Side Includes for this. Put the following in your .html file:

<!--#config timefmt="%d/%m/%Y" -->
Last modified at <!--#echo var="LAST_MODIFIED" -->

The first bit defines the format for the date.
In httpd.conf I turned on server parsing for .html files with

AddType text/html .html
AddHandler server-parsed .html

Perhaps you should load the SSI module explicitly as well.

---
"Perhaps nothing is true, and not even that!"
Multatuli, Ideen 1

[ Reply to This | # ]

Parsing .html files for php code
Authored by: iambradn on Jun 02, '03 04:03:52PM

In order to save load on your server, you could rename your .html files to .php files, and nest your php code in there.

Then, the interpreter will still only need to process .php files, and not all of the .html files.

.php files don't need to have php code in them. You can take any .html and rename it .php, and it will display (as long as your webserver can handle php pages).



[ Reply to This | # ]
Parsing .html files for php code
Authored by: kahless on Jun 03, '03 03:39:20PM

I could have done that. However, I would have had to have changed all of the links in my pages that refered to the renamed files. Granted I only have a hundred page site, but I didn't want to check all the pages manually (not all .html references would need to be changed to .php).

For a small established site my suggestion seems to be fine. I haven't noticed any performance decrease.



[ Reply to This | # ]
Parsing .html files for php code
Authored by: aranor on May 30, '03 05:41:06PM

I'd just like to note that the problem with parsing .html files for either SSI or PHP is that it slows down the serving of every single HTML page on the site regardless of whether it actually contains any SSI or PHP code, simply because every page has to be parsed for it now.



[ Reply to This | # ]
Parsing .html files for php code
Authored by: Beernd on May 31, '03 04:15:37AM

That's true, though it depends on your server how important this is.
To overcome this problem, one could put XBitHack on in httpd.conf. From then on, all files with their executable bit set will be server-parsed, and the others won't. That way you don't have to rename all the files containing SSI to their .shtml equivalent, you only need to set their x bit (chmod a+x file.html), and still not every .html file is parsed.

---
"Perhaps nothing is quite true, and not even that!"
Multatuli, Ideen 1

[ Reply to This | # ]

Parsing .html files for php code
Authored by: eo on May 31, '03 10:01:47AM

At the cost of using *slightly* more CPU time, I had PHP parse HTML files, and I turned on output compression with PHP. This had the overall effect of speeding up the site and using less bandwidth. I guess I could have just used mod_gzip to have Apache serve compressed files, but I also needed to add PHP code to thousands of HTML files without renaming them.



[ Reply to This | # ]
Parsing .html files for php code
Authored by: nwfrg on Jun 04, '09 10:24:13AM
If you configure Apache to parse .html files for PHP code be aware that you may lose the "Last-Modified" HTTP header. This is... by default Apache will include the "Last-Modified" info for .html files but it may not when it's parsing those .html files for PHP code. see: [link:]http://www.faqts.com/knowledge_base/view.phtml/aid/4674 for a work around.

[ Reply to This | # ]