downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | sites | links | conferences | my

search for in the

Keeping Current> <Disabling Magic Quotes
[edit] Last updated: Fri, 22 Jul 2011

view this page in

Hiding PHP

In general, security by obscurity is one of the weakest forms of security. But in some cases, every little bit of extra security is desirable.

A few simple techniques can help to hide PHP, possibly slowing down an attacker who is attempting to discover weaknesses in your system. By setting expose_php to off in your php.ini file, you reduce the amount of information available to them.

Another tactic is to configure web servers such as apache to parse different filetypes through PHP, either with an .htaccess directive, or in the apache configuration file itself. You can then use misleading file extensions:

Example #1 Hiding PHP as another language

# Make PHP code look like other code types
AddType application/x-httpd-php .asp .py .pl
Or obscure it completely:

Example #2 Using unknown types for PHP extensions

# Make PHP code look like unknown types
AddType application/x-httpd-php .bop .foo .133t
Or hide it as HTML code, which has a slight performance hit because all HTML will be parsed through the PHP engine:

Example #3 Using HTML types for PHP extensions

# Make all PHP code look like HTML
AddType application/x-httpd-php .htm .html
For this to work effectively, you must rename your PHP files with the above extensions. While it is a form of security through obscurity, it's a minor preventative measure with few drawbacks.

Keeping Current> <Disabling Magic Quotes
[edit] Last updated: Fri, 22 Jul 2011
add a note add a note User Contributed Notes Hiding PHP
CD001 21-Jul-2010 04:03
It's a good idea to "hide" PHP anyway so you can write a RESTful web application.

Using Apache Mod Rewrite:

RewriteEngine On
RewriteRule ^control/([^/]+)/(.*)$ sitecontroller.php?control=$1&query=$2

You then use a function like the following as a way to retrieve data (in a zero indexed fashion) from the $_GET superglobal.

function myGET() {
$aGet = array();

$_GET['query'])) {
$aGet = explode('/', $_GET['query']);


This is only a really basic example of course - you can do a lot with Mod Rewrite and a custom 'GET' function.
altan at javam dot org 18-Nov-2009 05:42
You can use this trick for non-direct used PHP files, eg. setting, class, ajax-related ones.

For abcde.php:

if ('abcde.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('What?');
sandaimespaceman at gmail dot com 27-Oct-2008 12:51
Set INI directive "expose_php" to "off" will also help.
You can spoof your PHP to ASP.NET by using:
header("X-Powered-By: ASP.NET");
Pyornide 11-Oct-2008 12:57
The idea of hiding the X-Powered-By in PHP is a flawed attempt at establishing security. As the manual indicates, obscurity is not security. If I were exploiting a site, I wouldn't check what scripting language the site runs on, because all that would matter to me is exploiting it. Hiding the fact that you use [x] language isn't going to prevent me from bypassing poor security.
Raz 24-Sep-2007 10:07
May some servers not allow you to put this line (i.e this not work)

AddType application/x-httpd-php .asp .py .pl
DefaultType application/x-httpd-php

so, the alternative method that really a good one is:

1- In your .htaccess file write:

RewriteEngine  on
RewriteBase  /dire/ or just /
RewriteRule  securename   yourfile\.php  [T=application/x-httpd-php]

example: all url like  parsed as

2- but here the $_GET not work, but $_POST work, so for dynamic pages like you use

now: instead of using $_GET use
$page        = strstr($uri, '=');
$page        = substr($page, 1);
$valid_pages = array('1', '2','...');
$page        = in_array($page, $valid_pages) ? $page : '1';

and for bad URL you can add this code to .htaccess file
of coarse below the first code in .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ [L]
prrogers at gmail dot com 13-Sep-2007 03:50
The default session identifier-name PHPSESSID is publicly visible in an HTTP cookie and or URL if sessions are used. It can be changed in the php.ini to something more generic to further obscure PHP.
rustamabd at google mail 26-Jan-2007 08:05
So far I haven't seen a working rewriter of /foo/bar into /foo/bar.php, so I created my own. It does work in top-level directory AND subdirectories and it doesn't need hardcoding the RewriteBase.


RewriteEngine on

# Rewrite /foo/bar to /foo/bar.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is /foo/bar.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

# NOTE! FOR APACHE ON WINDOWS: Add [NC] to RewriteCond like this:
# RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$" [NC]
simon at carbontwelevedesign dot co dot uk 10-Aug-2006 12:31
I use the following in the .htaccess document

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

then the following simple code


= explode("/",$_SERVER['REQUEST_URI']);

$varone = $permalinks[1];
$vartwo = $permalinks[2];


marpetr at NOSPAM dot gmail dot com 11-Apr-2006 12:18
I think the best way to hide PHP on Apache and Apache itself is this:

# ...
# Minimize 'Server' header information
ServerTokens Prod
# Disable server signature on server generated pages
ServerSignature Off
# ...
# Set default file type to PHP
DefaultType application/x-httpd-php
# ...

; ...
expose_php = Off
; ...

Now the URLs will look like this:

Now hacker knows only that you are using Apache.
eric at ericwing dot net 20-Jan-2006 05:20
Something that has not been mentioned here is also the PHPSESSION id that will be displayed in the URL when passing it from page to page using GET.  If users have cookies set to off, this will be visible. This can be reset before any session_start() call with ini_set(). Be aware however that this can't be changed in this way if you use autho session start.
user at pampelhuber dot invalid 18-Dec-2005 12:32
It is unnecessary, to let every Pampelhuber inspect your 'php.ini' files.
Put the following into the .htaccess of your htdocuments' root:

#Obscure 'php.ini' files (where they exist)
RedirectMatch 404 .*php\.ini$
jtw90210 30-Jun-2005 08:19
In order to get the PATH_INFO to work in order to pass parameters using a hidden program/trailing slash/"pretty url" in more recent versions of PHP you MUST add "AcceptPathInfo On" to your httpd.conf.

AddType application/x-httpd-php .php .html
AcceptPathInfo On

Try it out with your phpinfo page and you'll be able to search for PATH_INFO.

If you want to drop the .php use one or both of these:
DefaultType application/x-httpd-php
ForceType application/x-httpd-php
25-May-2005 08:06
You could also do this in .htaccess when you use Apache and your configuration allows you to override :

<Files test>
    ForceType application/x-httpd-php

That way, you can use the URL test?pop=true without having to fake it by using test/index.php.

See the Apache manual for more info:
benjamin at sonntag dot fr 24-May-2005 04:14
In response to the previous messages, for apache, there is a easier way to set files without "." to be executed by PHP, just put this in a ".htaccess" file :

DefaultType  application/x-httpd-php
dimitar at bastun dot net 17-Jan-2005 05:13
In case there are an Internal Server error(error 500) using the old code below in an .htaccess file, you can replace it with the code modification that must solve the problem.

Old code

<Files ~ "^[^\.]+$">
       ForceType application/x-httpd-php

Replacement of the code above(code modification)

AddHandler server-parsed .php
<Files ~ "^[^\.]+$">
SetHandler application/x-httpd-php

Dimitar Tanev
Nikolai-Zujev-(at)-Gmail-dot-Com 22-Sep-2004 07:22
Assign files w/o extension to php interpreter
without using ReWrite module

[clip httpd.conf]

<Files ~ "^[^\.]+$">
        ForceType application/x-httpd-php

php at vfmedia dot de 15-Jun-2004 01:21
Ive found an easy way to hide php code and the uri is searchable by google and others...(only for unix or linux)

At first I have some rules in my hide.conf (i made an extra .conf for it (apache 2.0))

For example when I want to mask the index.php

<Files index>
 ForceType application/x-httpd-php

My problem is, that my code should be readable...

so I made an extra folder for example srv/www/htdocs/static_output

My phpcode is in the includefolder....(for ex. mnt/source/index.php)

Then I made a link in the shell  > ln mnt/source/index.php srv/www/htdocs/static_output/index

So the code is readable (with .php extension) in my includefolder and there is only the link in the srv folder without extension(which is called by the browser...).
12-May-2004 03:20
Keep in mind, if your really freaked out over hiding PHP, GD will expose you.

Go ahead - make an image with GD and open with a text editor.. Somewhere in there you'll see a comment with gd & php all over it.
php at user dot net 11-Apr-2004 01:36
What about this in a .htaccess file :

RewriteEngine on
RewriteRule    ^$    /index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)/$    /$1/index.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)\.(html|htm)$    /$1.php    [L]
RewriteRule    ^([a-zA-Z0-9\-\_/]*)$    /$1.php    [L]

Typing "" loads "/anything/index.php" if 'anything' is a directory, else it loads "/anything.php".

I'm sure you can find mutch better, but it works great on my site :)
mmj 14-Mar-2004 01:58
You can see if somebody's using PHP just by adding the following to the end of the URL:
If the page is using PHP, this will show the PHP credits.

Setting expose_php to Off in php.ini prevents this.
ldemailly at qualysNOSPAM dot com 28-Oct-2003 04:17
adding MultiViews to your apache Options config
lets you hide/omit .php in the url without any rewriting, etc...
l0rdphi1 at liquefyr dot com 21-Jul-2003 11:02
More fun includes files without file extensions.

Simply add that ForceType application/x-httpd-php bit to an Apache .htaccess and you're set.

Oh yea, it gets even better when you play with stuff like the following:




foreach ( explode('/',$_SERVER['PATH_INFO']) as $pair ) {
$key,$value) = split('=',$pair,2);
$param[$key] = stripslashes($value);


Enjoy =)
Bryce Nesbitt at Obviously.COM 27-Mar-2003 04:24
Using the .php extension for all your scripts is not necessary, and in fact can be harmful (by exposing too much information about your server, and by limiting what you can do in the future without breaking links). There are several ways to hide your .php script extension:

(1) Don't hard code file types at all.  Don't specify any dots, and most web servers will automatically find your .php, .html, .pdf, .gif or other matching file. This is called canonical URL format:
This gives you great flexibility to change your mind in the future, and prevents Windows browsers from making improper assumptions about the file type.

(2) In an Apache .htaccess file use:
    RewriteEngine on
    RewriteRule page.html page.php

(3) Force the webserver to interpret ALL .html files as .php:
    AddType application/x-httpd-php .php3 .php .html
bminton at efn dot org 27-Feb-2003 08:05
Another technique is to have every file be named index.php and be in it's own directory.  Then instead of using for instance you could use where foo is a directory with a file called index.php in it.
29-Jan-2003 06:53
PS. If you want to use pretty URLs (i.e. hide your .php extensions) AND you have safe-mode=on, the previous example (ForceType) won't work for you.  The problem is that safe-mode forces Apache to honor trailing characters in a requested URL.  This means that:

would still be processed by the home script in our doc root, but for:

apache would actually look for the /home/contact_us.html file in our doc root.

The best solution I've found is to set up a virtual host (which I do for everything, even the default doc root) and override the trailing characters handling within the virtual host.  So, for a virtual host listening on port 8080, the apache directives would look like this:

<VirtualHost *:8080>
    DocumentRoot /web/doc_root
    Alias /home "/web/doc_root/home.php"
    AcceptPathInfo On

Some people might question why we are overriding the trailing characters handling (with the AcceptPathInfo directive) instead of just turning safe-mode=off.  The reason is that safe mode sets global limitations on the entire server, which can then be turned on or left off for each specific virtual host.  This is the equivilent of blocking all connections on a firewall, and then opening up only the ones you want, which is a lot safer than leaving everything open globally, and assuming your programmers will never overlook a possible security hole.
Azureash 28-Jan-2003 12:34
Another way to hide your .php extensions is to use the Apache ForceType directive (which is often referred to as pretty URLs.)  Basically you force Apache to parse a file as PHP that matches the trailing directory name in your URL.

For example, place this directive in your Apache httpd.conf file:
<Location /home>
     ForceType application/x-httpd-php

and create a php file name "home" in your doc root.  This file should not have a .php extension, and can be a php template file.  Combined with a function to strip out URL parameters, this can create a new templating system, which can effectively hide your file extensions.

In this example,

would actually use the home script we created, and then the "bar.html" could be used to specify content to include.
Kevin Vincent 23-Jan-2003 01:43
Just a thought but if you have changed the extensions that php interprets I would assume you've also changed header.php and footer.php files to the new extension.


index.php, somefile.php, header.php, footer.php...

Change the Apache directive so PHP interprets .kev files and rename your files:

index.kev, somefile.kev, header.kev, footer.kev

If you leave header and footer as PHP files then it won't understand how to interpret them.
sth at panix dot com 04-Aug-2002 07:45
The flipside to this is, if you're running a version of
PHP/Apache which is not known to have exploitable bugs (usually the latest stable version at the time), and an attacker sees this, they may give up before even trying. If they don't, they may continue to attempt their exploit(s).

It really depends on the type of attacker. The educated, security advisory reading attacker vs. script kiddie on the street.

If you're keeping up on patches, version exposition should not be a problem for you.
m1tk4 at hotmail dot com 23-Jul-2002 12:53
I usually do:

RewriteEngine on<br>
RewriteOptions inherit<br>
RewriteRule (.*)\.htm[l]?(.*) $1.php$2 [nocase]<br>

in .htaccess. You'll need mod_rewrite installed for this .
yasuo_ohgaki at yahoo dot com 26-Jan-2002 11:59
To hide PHP, you need following php.ini settings


and in httpd.conf

ServerSignature Off
(min works, but I prefer off)
istvan dot takacsNOSPAM at hungax dot com 30-Dec-2001 05:42
And use the
ServerTokens min
directive in your httpd.conf to hide installed PHP modules in apache.

show source | credits | sitemap | contact | advertising | mirror sites