Understanding $_SERVER['PHP_SELF'], $PHP_SELF, $_SERVER['REQUEST_URI'] and $_SERVER['SCRIPT_NAME'] in PHP and when to use what
The above PHP variables have slight differences which are often ignored, often leading to problems in large scale deployments or product solutions. I have attempted to debug them for you.
$_SERVER['SCRIPT_NAME']
This contains the current script's path. If you access http://blog.taragana.com/ or http://blog.taragana.com/index.php the $_SERVER['SCRIPT_NAME'] will be same - /index.php. It is irrespective of the actual URI ($_SERVER['REQUEST_URI']) used to access the site.
As it returns the actual script name, it fails provide additional path information that may be present. So if the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then too the $_SERVER['SCRIPT_NAME'] will be same - /index.php.
$_SERVER['SCRIPT_NAME'] is supported on all platforms
$_SERVER['PHP_SELF']
This is the filename of the currently executing script, relative to the document root. However, unlike $_SERVER['SCRIPT_NAME'], it provides additional path information like $_SERVER['REQUEST_URI'] when the actual php file is present in the path. So when the $_SERVER['REQUEST_URI'] is /index.php/big/directory/ then $_SERVER['PHP_SELF'] will be /index.php/big/directory/.
However if all the URI's under http://www.example.com/ is mapped to http://www.example.com/index.php, then, for example, http://www.example.com/abc/def will return /index.php like $_SERVER['SCRIPT_NAME']. Note that $_SERVER['REQUEST_URI'] data is ignored for this request.
$_SERVER['PHP_SELF'] is supported on all platforms.
Pages: 1 2
Filed under Headline News, How To, PHP, Web, Web Hosting, Web Services |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |





































June 15th, 2006 at 3:45 pm
$_SERVER variables not set by Tomcat
Not able to get the value for variable in Tomcat 5.5 and PHP Version 5.1.4 configuration
In fact, only values in _SERVER are
_SERVER is array 3
Key: REQUEST_TIME value: 1150399842
Key: argv value: Array
Key: argc value: 0
Searched but couldn’t find anything relevant. Any pointers?
August 17th, 2006 at 12:08 am
Hey…I was wondering how to call a particular function by using the $_SERVER['PHP_SELF'] command. Is there any supported way of calling a function from this?
-Dan P.
December 3rd, 2006 at 4:40 am
hey,
I have a problem in which the url isn’t parsed properly so it doesnt add the .php to the end of the address in the url bar. for example, http://www.mysite.com/test/11 is suppose to be http://www.mysite.com/test/11.php the .php is missing.
My code:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
AddType text/xml .xslt
SetHandler text/xml
# PHP is in CGI Mode, so we need a different mod_rewrite
RewriteCond %{REQUEST_URI} !categories\.php
RewriteRule ^categories(.*) /categories.php?$1 [L]
RewriteCond %{REQUEST_URI} !questions\.php
RewriteRule ^questions(.*)$ /questions.php?$1 [L]
RewriteCond %{REQUEST_URI} !search\.php
RewriteRule ^search(.*)$ /search.php?$1 [L]
December 20th, 2006 at 12:55 pm
Hi there,
I need to retrieve just the page name from the URL - anyone knows how could I do it?
I want then assing its value to a variable and use it in sql query for comparasion.
Any help would be appreciated.
I look forward.
Sebastian
May 2nd, 2007 at 11:33 pm
how can i get the file name without the directory name
June 19th, 2007 at 8:05 am
subash have searching ajob
August 7th, 2007 at 6:42 am
Is this better to call php_self function or create a new file.Because when people refresh it submit the message twice, how can overcome this issue.
October 8th, 2007 at 6:22 am
Hello..
I’ve done with a program which send copy from local machine to server.
I m having problem while copying the file. Its copied, but goes in Apache server. Please help me to copy that file in server(www)folder.
November 1st, 2007 at 1:24 am
Very useful … did clarify some technicalities for me. Thanks for the headsup! =)
February 17th, 2008 at 7:00 am
While I am excecuting $_SERVER['PHP_SELF']then I am getting one message in my browser Forbidden
You don’t have permission to access /
March 10th, 2008 at 10:02 am
thankyou for the informative article.
June 10th, 2008 at 8:25 am
Hi the inforamtin on this site is really helpfull
Thanks
! BYE