473,489 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
"www.server.com/theFile.php" would show the end result of the php
actions, whereas going to "www.server.com/theFile.phps" 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 %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-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.

Thanks

Jan 26 '07 #1
3 2047
Rik
On Fri, 26 Jan 2007 02:04:18 +0100, Lucanos <lu*****@gmail.comwrote:
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
"www.server.com/theFile.php" would show the end result of the php
actions, whereas going to "www.server.com/theFile.phps" 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 %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-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.
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
see.
Now, for your solution:

Make 2 files:
----highlighter.php-----
highlight_file($_GET['file']);
------------------------

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

Voilà.
--
Rik Wasmus
Jan 26 '07 #2
Lucanos wrote:
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).
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
people.

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:

<?php
if ($_GET['source'])
{
highlight_file($_SERVER['SCRIPT_FILENAME']);
exit();
}
?>

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

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jan 26 '07 #3
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
"www.server.com/theFile.php" would show the end result of the php
actions, whereas going to "www.server.com/theFile.phps" would show the
actual content of the file itself.

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
www.server.com/showsource?file=theFile.php

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

Best regards
Jan 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2899
by: LP | last post by:
Hi, I have a site which runs on .Net technology. The site is already deployed and running. I need to change 1 file for some small enhancement. Please tell me, how can I compile that 1 file and...
2
3714
by: Jerry | last post by:
Hi all! I have a small website and am having a slight difficulty keeping up with the changes needed to keep it current and fresh. I have a lot a archived pages that I'd like to rotate maybe in...
9
6623
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
10
2284
by: Tim Mulholland | last post by:
My company is about to begin working on an ASP.NET application. There are going to be two primary developers working on this project. It will be a fairly deep project (lots of lines of code) but...
3
1968
by: Disco Octopus | last post by:
Hi, Does anyone know if there is a way to get something equivilent to this... AddType application/x-httpd-php-source .phps .... to work for IIS? Thanks
0
2008
by: thjwong | last post by:
I'm using WinXP with Microsoft Visual C++ .NET 69462-006-3405781-18776, Microsoft Development Environment 2003 Version 7.1.3088, Microsoft .NET Framework 1.1 Version 1.1.4322 SP1 Most developers...
6
1473
by: Raistlin Majere | last post by:
I have Apache/MySQL/PHP on Windows XP. I want my PHP files to behave in my computer like in web host. I open a PHP file with FF and its content is printed!
4
4439
by: Alan Mailer | last post by:
Again, I'm new to VB.net and there is something I need help with: Like (I assume) many of us, over time I want to be able to create some VB.net classes that I might want to use in more than one...
4
1207
by: Horacius ReX | last post by:
Hi, I have a C program split into different source files. I am trying a new compiler and for some reason it only accepts a single source file. So I need to "mix" all my different C source files...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.