What is the difference between ["PHP_SELF"], ["SCRIPT_NAME"] and ["REQUEST_URI"]?

March 15th, 2007 by Quatrux

What is the difference between ‘PHP_SELF’, ‘SCRIPT_NAME’ and ‘REQUEST_URI’ in the superglobal $_SERVER[] or also known as $HTTP_SERVER_VARS which is deprecated since PHP 4.1.0 and isn’t really a superglobal, but anyway, what is the difference? On different support channels and forums people ask this question and I tried to search google and didn’t find an answer, so I thought to write it here.


The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar.

  • http://example.com/ — – — /index.php
  • http://example.com/test/index.php — – — /test/index.php
  • http://example.com/index.php?q=submit — – — /index.php
  • http://example.com/index.php/test/ — – — /index.php/test

So actually it returns the path to the current filename typed in the url without the QUERY_STRING, but when we us index.php/test/download/ it shows the path to ../test/download/ as we want it to do that, because usually using PHP_SELF is very useful with submitting forms, so having the PATH_INFO included in the PHP_SELF is a good idea and if you don’t want it, just use SCRIPT_NAME which will always show the executing file.


Contains the current script’s path. This is useful for pages which need to point to themselves.

  • http://example.com/ — – — /index.php
  • http://example.com/test/index.php — – — /test/index.php
  • http://example.com/index.php?q=submit — – — /index.php
  • http://example.com/index.php/test/ — – — /index.php/

So it is similar to PHP_SELF, just the PATH_INFO isn’t included, so you just point to the current filename executing. Note; that SCRIPT_FILENAME actually is the same, but it also returns the server root and acts the same as $_SERVER['DOCUMENT_ROOT']; . ” . $_SERVER['SCRIPT_NAME'];


The URI which was given in order to access this page; for instance, ‘/index.html’.

A lot of people usually don’t understand the usage of this, for example if you wanted to access index.html but it didn’t exist on the server, you get redirected to say 404.php page which says that the filename doesn’t exist, when you will try to use any other value, like PHP_SELF or SCRIPT_NAME it will print you /404.php and not the file you requested, but when you will use REQUEST_URI, it will print the full URL which you typed in order to get to that page, this sometimes is quite useful.

In addition, I recommend to use the constant __FILE__ if you want to get the absolute path to your current dir and if you want to set an include path, it is best to do it also with this constant by adding the function dirname(); like this: dirname(__FILE__);

  1. /* Change Super Global */
  2. $s =& $_SERVER;
  3. /* Set the Default Include Path */
  4. set_include_path( dirname( $s[‘SCRIPT_FILENAME’] ).‘/includes’. PATH_SEPARATOR . dirname(__FILE__) );

If you ask if this is different, when I will say yes it is. The first value points to the currently executing file like /index.php and the second value is pointing to the current included file directory, it even can be outside /public_html/ somewhere in /home/user

Sometimes people don’t want to get the filename, but only the Query of the GET method, so there is a value $_SERVER['QUERY_STRING']; which returns everything after the ?

  • http://example.com// — – — (nothing)
  • http://example.com/test/index.php — – — (nothing)
  • http://example.com/index.php?q=submit — – — q=submit
  • http://example.com/index.php/test/ — – — (nothing)
  • http://example.com/index.php/test/?q=submit — – — q=submit

Furthermore, if you want to get the server root, you can use $_SERVER['DOCUMENT_ROOT']; which usually returns something like /home/user/public_html/

Moreover, sometimes you can have an headache if you want to get the domain the script is currently running on, you ask why? because sometimes you can use not the right $_SERVER array key for that.. if you’ll be using $_SERVER['SERVER_NAME']; you will get the domain of the server, but say you’re using CPanel and added a domain through it and you will execute under it, you will still get the original server domain, this is called virtual host, in order to get it you need to use $_SERVER['HTTP_HOST'];

Read more about Reserved Server Variables on PHP dot net Manual!

Posted in PHP | No Comments »

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment and don't be afraid of the spam protection!