Single PHP file - *.php = process, *.phps = source.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi All,

Hoping for some direction here (I have searched, without avail, for a
solution already).

I was toying with trying to find a way whereby I could have a single
PHP file, and then specify whether to either see the processed output
or the source code by using the extensions php and phps (respectively).

So, with one file called "theFile.php" on the server, going to
"" would show the end result of the php
actions, whereas going to "" would show the
actual content of the file itself.

I thought this could be done by modifying the htaccess file as follows:

AddType application/x-httpd-php-source phps

RewriteEngine on
RewriteCond % !-f
RewriteCond % !-d
RewriteRule ^(.+)\.phps$ $1.php

However this does not work:
- if a *.php file exists, then accessing *.php or *.phps returns the
end result of the php actions.
- if a *.phps file exists, then accessing *.php returns a 404 error,
and *.phps shows the actual file contents.

Looking for any direction, references, or assistance.


Re: Single PHP file - *.php = process, *.phps = source.

Quoted text here. Click to load it

First of all, let's make it clear that what you want is a major, major  
security risk... Add very rigid security fot the ones allowed to see the  
code, both in authenticating and in what directories they are allowed to  

Now, for your solution:

Make 2 files:

RewriteCond $1.php -f
RewriteCond $1.phps !-f
RewriteRule ^(.*)\.phps$ highlighter.php?file=$1

VoilĂ .
Rik Wasmus

Re: Single PHP file - *.php = process, *.phps = source.

Lucanos wrote:

Quoted text here. Click to load it

As Rik said, for production code this can cause security problems. Of
course, properly secured code doesn't rely on security-through-obscurity,
but still, obscurity helps sometimes.

That said, I often use it as a technique when posting example code for

Method one: symbolic links. Create the file mycode.php, then create a
symbolic link to it using the following command and the command line:

    ln -s mycode.php mycode.phps

This effectively creates two copies of the file, one called "mycode.php"
and one called "mycode.phps", but any updates to the PHP file will also
show up in the PHPS file.

Method two: PHP. Add the following code to the top of each PHP file:

        if ($_GET['source'])

You can now add "?source=1" to a URL to show its source.

Toby A Inkster BSc (Hons) ARCS
Contact Me  ~

Re: Single PHP file - *.php = process, *.phps = source.

Quoted text here. Click to load it

I would do this in another way: create a PHP script that takes a  
filename as a parameter, checks this to see if it is a file that is  
allowed to be seen, and sends the contents of the file as plaintext to  
the browser.

You would call it with something like

Of course, you can instruct apache to turn that into any other URL with  

Best regards

Site Timeline