Displaying (vs executing) PHP code on web pages

While working on an earlier post, I needed to find a way to display a PHP module on a web page without having it executed. I knew this was fairly simple and involved the .phps file extension, but I hadn’t done it before. A quick search of the PHP site pointed me to the highlight_file function. About halfway down the page there is a blurb about the MIME type that I needed. It turns out that my web hosting service already has this enabled, so all I needed to do was get a .phps extension on my file. I did it the easy way, with a softlink:

ln -s myfile.php myfile.phps

For those who don’t know Unix, this effectively creates another name for the same file, which is exactly what I want. This leaves the original .php alone, so that it can be run by the web server. But is also allows me to post the code on the web, using the .phps “file” which the web server will display instead of executing. The elegance of this solution is that if/when I modify the original code, I won’t have to make any changes to the displayable code because there is really only one file (with two distinct names).

About Jim Vanderveen

I'm a bit of a Renaissance man, with far too many hobbies for my free time! But more important than any hobby is my family. My proudest accomplishment has been raising some great kids! And somehow convincing my wife to put up with me since 1988. ;)
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *