Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 5.4Superglobals in PHP

Part 5.4Superglobals in PHP

PHP has many superglobals which are essentially variables that are are global to every script.

Some superglobals have already been used in this tutorial including:

  • $_GET
  • $_POST
  • $_REQUEST
  • $_SESSION
  • $_COOKIE

This article in this tutorial will focus on the superglobals in the PHP language. These are global variables that can be accessed anywhere within the program.

$_SERVER

The $_SERVER superglobal is used to access information about the server that is processing the page, the page that is processing the page or the client system that is requesting the page.

The following table lists indexes for the $_SERVER superglobal (the $_SERVER superglobal is an associative array):

Parameter Purpose
DOCUMENT_ROOT Gets the web server root. In shared hosting packages, it will sometimes give the user's root.
HTTP_REFERER Attempts to retrieve the page that sent the request to the current page.
PHP_SELF The file path of the current script.
QUERY_STRING Gets the query string of the request to the script.
SCRIPT_FILENAME The absolute pathname of the file where the currently executing script resides.
SCRIPT_NAME Gets the current scripts path including it's name.
SERVER_ADDR Gets the IP address of the web server running the script.

DOCUMENT_ROOT

Consider a file that is to be reused is located on a webserver at the following address:

http://www.samplesite.com/php/includefile.php

Now consider a file that is located at this address:

http://www.samplesite.com/blog/2011/04/windows-xp.htm

If the file in the first URI is to be included in the second page, the file would have to back reference it's parent folder's parent folder's parent folder. This could be achieved by using the shell-style up directory directive (".."):

PHP
<?php
	include '../../../../php/includefile.php';
?>
		

This gets messy and goes wrong very easily. The root of the server can be accessed quite easily using the 'DOCUMENT_ROOT' string within the $_SERVER superglobal.

PHP
<?php
	include $_SERVER['DOCUMENT_ROOT'].'/php/includefile.php';
?>
		

There also exists a __FILE__ magic constant. This will give the full name of the file that the script is executing from, and even give the path of the included path of any included file that calls it.

HTTP_REFERER

The 'HTTP_REFERER' index will attempt to return the full path of the page that directed the browser to this page. Some browsers do not send this information and therefore this is not a recommended way of assessing this.

PHP_SELF

The 'PHP_SELF' index will return the file path of the script that calls on this. This is very similar to the __FILE__ magic constant.

QUERY_STRING

Another very useful index on the $_SERVER is the 'QUERY_STRING'.

This index returns the query string that was used to access the page. For instance, applied to the following string

http://www.example.com/?img=43

will return just the part in bold.

This can be useful for two reasons. The first reason is to get the query string for whatever reason and the second is to remove the query string assuming the full URL of the current page also includes the query string:

PHP
<?php
	$query = $_SERVER['QUERY_STRING'];
	$url = str_replace($query, "", $url);
?>
		

SCRIPT_FILENAME

The 'SCRIPT_FILENAME' index will return the full path of the executing script. This means that if the file is located at /home/username/documents/server/about/index.php it will return exactly that.

SCRIPT_NAME

The 'SCRIPT_NAME' index will return the relative path of the script. So for instance, if the server root is /home/username/documents/server/, and the same file as used in the previous example (/home/username/documents/server/about/index.php) requests this index, it will return just /about/index.php.

SERVER_ADDR

The 'SERVER_ADDR' will return the IP address of the server of the server which is executing the script.

More $_SERVER indexes

There are many other $_SERVER indexes of which those not covered here (the less commonly used) are described here:

http://php.net/manual/en/reserved.variables.server.php

$GLOBALS

PHP also has a $GLOBALS superglobal. This interesting superglobal is used to access global variable that exist when local variables of the same name exist. Consider the following:

PHP
<?php
	function test() {
		$var = "Hello world";
		echo $var;
	}
	$var = "Hello Adam!";
	//Run test function
	test();
?>
		

The echo here will only output the local version of $var as it cannot tell the difference between the local and global variable.

The next example shows how the $GLOBALS overcomes this problem:

PHP
<?php
	function test() {
		$var = "Hello world";
		echo $var;
		echo $GLOBALS["var"];
	}
	$var = "Hello Adam!";
	//Run test function
	test();
?>
		

More superglobals

As mentioned at the start of this article, the $_SERVER superglobal is perhaps the most obvious and powerful superglobal. There are other superglobals, but they are limited in use and not covered in this tutorial. The other two which are not covered in this tutorial are:

  • $_ENV
  • $_FILES
Feedback 👍
Comments are sent via email to me.