Download | Software submit | Magazine | Help | My account
 
SoftSpecialist
 
Programs Games Reviews Mobile Online

Soft Specialist Software Magazine

PHP $_SERVER variables comparison

$_SERVER['REQUEST_URI'] - $_SERVER['PHP_SELF'] - $_SERVER['SCRIPT_NAME']

PHP Tutorials | All Articles
 

 


$_SERVER is a PHP array which contains information collected by the web server, frequently vital for your PHP script, like headers, paths or script locations.

$_SERVER['REQUEST_URI'], $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] are homogenous PHP server variables ($_SERVER).

In many cases $_SERVER['REQUEST_URI'] will contain the same value with $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'], but sometimes they may differ. However, in all the cases the information is about the currently executing php script.

The goal of this PHP tutorial is to identify the different practical behaviors of these three important PHP variables.

According to PHP help, 'PHP_SELF' returns the filename of the currently executing script, relative to the document root. Let's take some practical examples:

http://www.sample-website.com/php-server-variables/ >>> / php-server-variables/index.php
http://www.sample-website.com/php-server-variables/script.php >>> / php-server-variables/script.php
http://www.sample-website.com/php-server-variables/script.php?var=help >>> /php-server-variables/script.php
http://www.sample-website.com/php-server-variables/script.php/my-php-tutorial >>> /my-php-tutorial

In the first three examples $_SERVER['PHP_SELF'] variable returned the filename of the currently executing script relative to the document root without taking into account the arguments variables. However, in the fourth php example it behaved differently.


The PHP help says that 'REQUEST_URI' gives you the URI which was entered in order to access the page:

http://www.sample-website.com/variables/ >>> /
http://www.sample-website.com/variables/script.php >>> /variables/index.php
http://www.sample-website.com/variables/script.php?var=help >>> /variables/script.php?var=help
http://www.sample-website.com/variables/script.php/my-php-tutorial >>> /variables/script.php/ my-php-tutorial

In all the cases, $_SERVER['REQUEST_URI'] returns exactly what is entered in the browser URL. This can be very useful to retrieve the exact URL, for example when using modrewrite (mod rewrite).

In PHP help, 'SCRIPT_NAME' variable is described as containing the current script's path:

http://www.sample-website.com/server-variables/ >>> /server-variables/index.php
http://www.sample-website.com/server-variables/index.php >>> /server-variables/index.php
http://www.sample-website.com/server-variables/index.php?var=help >>> /server-variables/index.php
http://www.sample-website.com/server-variables/index.php/my-php-tutorial >>> /server-variables/index.php

As we can see, $_SERVER['SCRIPT_NAME'] always returns php script name and only the php script name. This is useful for php pages which need to point to themselves.

More PHP tutorials in the next SoftSpecialist articles. Until next time Keep Safe! Safe websites and safe php scripts.

 
Copyright © 2026 SoftSpecialist.com
Print this article

This article or portions of it may not be reproduced without prior consent from SoftSpecialist.com
However, you are free to link to the article.

Spread the word! Paste into your website, blog or forum the code below to link to this interesting article:



Related Articles


SoftSpecialist PHP Tutorials Downloads


Other Soft Specialist Software Downloads

 
 
 
  Audio
  Business
  Desktop Customizing
  Education
  Email, Chat, Phone
  Games
  Graphics
  Hobby & Home
  Internet
  Network Toolbox
  Programming
  Security-Privacy PC/Internet
  System Utilities
  Video
  Web Designers





     
  Subscribe to newsletter:  
 
 
     

      SoftSpecialistTM and SoftSpecialist InstituteTM are registered trademarks.
      © 2026 SoftSpecialist. All rights reserved.   Privacy Policy | Terms of Use | Contact