Advice, tutorials and tips for beginner and experienced software/web application developers
The core of server side web development is of course producing dynamic content for your users. Doing this has allowed us to serve to our users exactly what they are looking for, created at run time specifically for them. Doing this with HTML is very common across the world but a technique that not all developers are aware of or (most likely) don't take advantage of is doing this with their other files.
Why would I want to Generate Anything other than HTML at Run Time?
How do I do it?
Executing PHP from within other files is as simple as adding one file to your htaccess file. I would recommend creating separate htaccess files for your script folders so that they do not damage any other instances of these file types on your site which do not need to be parsed as PHP. I make this point since the header on an XML file will be parsed as PHP and will error if it is not dealt with correctly.
If you do not already have a htaccess file, or have not used them before create a blank text file named ".htaccess" (note the lack of a file extension) in the folder which contains your scripts. In that text file put the line below which best suits you. You can mix and match these rules all on one line depending on which file extension(s) you are trying to parse as PHP.
AddHandler application/x-httpd-php .js
AddHandler application/x-httpd-php .css
AddHandler application/x-httpd-php .xml
#All Three of the Above on one line
AddHandler application/x-httpd-php .js .css .xml
The lines above will now make your server execute any files matching the extensions defined, within the current and sub directories as a PHP file. You can also use the AddType directive as opposed to the AddHandler but these both have a similar effect, it is generally seen as better practice to use AddHandler.
In order to make sure the users browser can properly understand your new dynamic files make sure you use the header function in PHP to inform the browser of the files mime type. This should go before any output begins to avoid errors.
I have Added the AddHandler Command to my htaccess File but it Does not Work
On some server installations this command is ignored because of other conflicting configuration settings. There is however a work around for this scenario which involves using url rewriting. To do this we first must make PHP files which will act as our dynamic files. Set these files up so they execute exactly how you want your files to do so. We then add a rewrite rule to our htaccess file which masks the fact that our files are really PHP files and let us reference them as whatever file extension we desire.
In this example I have a dynamic CSS file which at the moment is in the css folder and is called style.php. We want to link to this in a web page as style.css in the same folder so it appears to be a regular static stylesheet. To do so we add the following line to our htaccess file.
RewriteRule ^css/style.css /css/style.php
Now when a request for style.css is made the dynamically generated contents of style.php are sent. This is an easy way to achieve the same result but without using the AddHandler command as most server configurations will allow the RewriteRule command.
This is just one a various ways you can use your apache configuration to add value and functionality to your site. If you have any problems with the above or it is not working on your server setup feel free to comment below and I will do my best to help you to get it working.
Sorry comments are currently disabled for maintenence
5 Most Recent Articles
An article for users of MySQL databases describing how they can use advanced stored procedures to improve efficiently in their applications.
A guide for LAMP developers to using stored procedures in MySQL and extending the MySQLi class.
An introduction to using the xlwt and xlrd modules for python to interact with Microsoft Excel spreadsheets.
This is an introduction to making HTTP requests from a python script/application using httplib.