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 Network
Friday, 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.

Discussion

habeeb perwad
June 2, 2010: 11:32 am

Very useful. lot of thanks


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.
the code in question is the $_SERVER['PHP_SELF']….

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
November 2, 2009: 9:36 am

Hmmm….
Thank you for sharing. Its a great help!

September 17, 2009: 8:28 am

Thanks Now my problem have solved

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!

June 3, 2009: 5:58 pm

thanks for sharing valuable information.


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.

February 25, 2009: 6:44 pm

nicely explained! very helpful

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

October 15, 2008: 9:16 am

Great explanation!

It’s easy to get mixed up and get errors lol.

Thanks ;)


Amit
June 10, 2008: 8:25 am

Hi the inforamtin on this site is really helpfull
Thanks
! BYE

March 10, 2008: 10:02 am

thankyou for the informative article.


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..
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.

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.


ss
June 19, 2007: 8:05 am

subash have searching ajob


SATISH
May 2, 2007: 11:33 pm

how can i get the file name without the directory name


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?
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


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:
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]


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
Key: REQUEST_TIME value: 1150399842
Key: argv value: Array
Key: argc value: 0

Searched but couldn’t find anything relevant. Any pointers?

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :