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