Understanding $_SERVER['PHP_SELF'], $PHP_SELF, $_SERVER['REQUEST_URI'] and $_SERVER['SCRIPT_NAME'] in PHP and when to use what
By Angsuman Chakraborty, Gaea News NetworkFriday, June 3, 2005
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 https://blog.taragana.com/ or https://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 https://www.example.com/ is mapped to https://www.example.com/index.php, then, for example, https://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.
neal jones |
May 23, 2010: 7:49 am
I keep on seeing this php code and when i use it i get nothing but errors.I have php 5.3 the latest. In Apache do I have to turn on the super globals or register globals or is it the latest software no longer supports the php command. |
Best Free Ads |
September 1, 2009: 9:56 am
Thank you for sharing this knowledge.. I only know about PHP_SELF before.. Now I know the other type.. |
September 1, 2009: 7:27 am
Thank You to shrae the information about PHP_SELF and the other.. Recently I have a problem about this and get confuse to differentiate between it.. |
asdf |
August 18, 2009: 1:54 am
ah! thank you, im still slightly unfamiliar with some php issues i run across but this cleared the current issue up, thanks! |
Ambareesh Shrivastav |
March 2, 2009: 1:57 am
I use a $page_name at the top of all my PHP files..it makes things really simple. I do use $_SERVER['REQUEST_URI'] and PATH_INFO , but only when I really need them. |
December 20, 2008: 8:59 am
i have same problem with “george” While I am excecuting $_SERVER['PHP_SELF']then I am getting one message in my browser Forbidden |
Amit |
George |
February 17, 2008: 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 / |
November 1, 2007: 1:24 am
Very useful … did clarify some technicalities for me. Thanks for the headsup! =) |
Prasad Patil |
October 8, 2007: 6:22 am
Hello.. |
August 7, 2007: 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. |
SATISH |
Sebastian |
December 20, 2006: 12:55 pm
Hi there, I need to retrieve just the page name from the URL - anyone knows how could I do it? Any help would be appreciated. Sebastian |
Dan |
December 3, 2006: 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, https://www.mysite.com/test/11 is suppose to be https://www.mysite.com/test/11.php the .php is missing. My code: SetHandler text/xml # PHP is in CGI Mode, so we need a different mod_rewrite |
DAN |
August 17, 2006: 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. |
vijay |
June 15, 2006: 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 Searched but couldn’t find anything relevant. Any pointers? |
habeeb perwad