Pre-processing with PHP
5 09 2010For me, including snippets of PHP code within my web-pages has always been a given. To the extent that it sometimes comes as a surprise to me when someone asks me how to do it. Doesn’t everyone already know?
What most people trip up on is expecting PHP to only operate on files with a .php suffix. Nothing wrong with this so far as it goes, but I prefer my web pages to have .html suffixes. The fact that I choose to embed chunks of PHP inside my pages needn’t be visible to the end-user. Or rather, viewer. So how do I do it?
Well, it couldn’t be easier, really.
But first, a small refresher on how PHP embeds itself into web pages anyway. I use Apache but all HTTP Server software will work in much the same way.
The file Apache uses as it configuration file is invariably called httpd.conf. It’s location may sometimes be a little obscure, dependent on what OS it is residing on – on my Red Hat servers (well CentOS, anyway!) it is found in /etc/httpd/conf/. Caveat: some Ubuntu distros seem to want to call the file apache2.conf – but you shouldn’t have much trouble figuring that out, especially if you remember that GIYF (Google Is Your Friend)
Anyway, a well-behaved PHP install will create or amend something like the following entries in said configuration file…
<IfModule dir_module>
DirectoryIndex index.php index.cgi index.html index.htm
</IfModule>
.
AddHandler test/html .htm .html
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
.
LoadModule php5_module libexec/apache22/libphp5.so
.
.
In brief, PHP has told Apache that
- accept index.php as a valid directory index file. It is now the same as index.html in that respect.
- Anytime Apache processes a file with a .php suffix, use the PHP MIME-type declared as the processing agent.
- Load in the PHP module that does all this good stuff.
OK, so at this stage, a file called say, example.php will be correctly processed and served by Apache. But I ALL my .html files pre-processed by PHP. Simples.
AddType application/x-httpd-php .php .htm .html AddType application/x-httpd-php-source .phps
After saving these changes, checking no silly typos were accidentally entered into the config file…
root: apachectl -t Syntax OK
…and then restarting Apache
root: apachectl graceful
Upon which, all things being equal, PHP will pre-process all .htm and .html files. Obviously, there is an overhead in doing this but you’d have to be running a seriously hard-run site before this became an issue. Do remember, after all, that PHP has been highly optimised to integrate into HTML and I’m willing to bet you’ll not notice a difference in page load times (the time taken to download images will far outweigh any time spent by PHP doing it’s stuff)
Finally, note that AddType and AddHandler ARE different! As a very rough rule of thumb, I try and use AddType exclusively. FWIW, the difference is that AddHandler directives tell Apache how to handle file types and AddType directives tell Apache how to handle the contents of files. Any clearer? No, thought not. As I said, try and use AddType directives exclusively and you’re not likely to go far wrong.
Categories : technical












Recent Comments