Archive for the ‘PHP’ Category

Templating in PHP in 10 seconds flat

December 6th, 2008 No comments

One of the rather messy practices that the PHP documentation actually encourages is the practice of using include or require to correctly layout a web page.

The PHP manual suggests that two pages be created: header and footer, and that these are included at the beginning and end of each document.

Thus for a ‘contact us’ page the contact.php file would look something like:

<? include("header.php") ?>
... The contact pages HTML ...
<? include("footer.php") ?>

While this does work, there are easier ways to accomplish the same thing and still maintain compatibility with editors such as Dreamweaver which seems to choke on even a small amount of PHP.

The under-documented auto_prepend_value and auto_append_values get a brief mention in the ini directives documentation but I thought it would be nice to show an example of how these can be used in the wild,

Adding the following to either an existing ‘.htaccess’ (note the starting dot) or downloading the below file will do pretty much the same as the above contact.php file example did only without needing to specify the files in the beginning and ends of each separate file.

?Download .htaccess
php_value auto_prepend_file "header.php"
php_value auto_append_file "footer.php"

And as with all .htaccess directives you can override these on a directory basis. So if you need a different header and footer when inside the ‘invoices’ directory dump another copy of the above into that directory changing it as needed.

And yes this method can be merged with the previous post to work with the URL prettifier.

Categories: HowTo's, PHP Tags: , ,

URL prettification in 10 seconds flat

November 29th, 2008 No comments

A word to all PHP, ASP, Python and Perl web-devs out there:

Is soooo 1990’s.

URL prettification has always been seen to be the exclusive domain of larger frameworks but with Apache it really isn’t that hard.

Simply paste the following into a file named ‘.htaccess’ (note the starting ‘dot’) and dump it in your website to allow the extension of your script to be ‘guessed’ by Apache should it not be provided.

This hack obviously requires mod_rewrite but since every hosting provider and their dog provides this these days i would say not having it enabled would be the exception rather than the rule

?Download .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$		$1.php [L]

ASP, Python and Perl people either add your .asp, .py or .pl sections under or above the .php bits or simply replace them.

There, now we can use:

But the old way (with .php extension) is still valid too.

Finally we put an end to this silly file extension nastiness.

Categories: HowTo's, PHP Tags: , ,