counter customizable free hit

PHP Superglobals Tutorial


This PHP tutorial will go over some of the basics of PHP but it is also one of the most important aspects of programming with PHP. We are talking about PHP Superglobals which are the built-in variables that are always accessible, regardless of scope and can be using in function, class or file. There are a total of eight Superglobals that we will go over in this tutorial that will help improve your PHP coding.

  • $_SERVER
  • $_POST
  • $_GET
  • $_FILES
  • $_COOKIE

Let’s go over the purpose of each Superglobal variable and give some examples of how you would use it in a real programming situation.


$GLOBALS (as the name suggest) is a variable which is used to access global vairables from anywhere in the PHP as well as within your function. Any and all global variables that you create outside the scope can be called with $GLOBALS[index] (the index will be the name of the variable you want to call). Here is an example.

A very handy way of calling a varaible outside of the scope.


The $_SERVER Superglobal variable is a global variable that holds information about the headers, paths, and script locations that is normally used to display or call information regarding the server your site it hosted on. The $_Server accepts a list of elements that can be passed to help designate the information you are trying to retrieve. Here are a few.

$_SERVER[‘PHP_SELF’] // Returns the filename of the currently executing script
$_SERVER[‘SERVER_NAME’] // Returns the name of the host server
$_SERVER[‘HTTP_HOST’] // Returns the Host header from the current request
$_SERVER[‘HTTP_REFERER’] // Returns the complete URL of the current page
$_SERVER[‘SCRIPT_NAME’] // Returns the path of the current script


The PHP $_REQUEST Superglobal is used to collect data after submitting an HTML form. $_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET and is best used on self referencing forms for validation. See the example below. We use the $_REQUEST[] to request the name from the input with the method “fname”.


The $_POST Superglobal variable is used to collect data from a form when the method=”post”. Here is our example of a form submitting the data to itself.


The $_GET Supergloabl variable is similar to the $_POST variable but the data is retrieved from the methods within the URL. Here is an example.


The $_FILES Superglobal variable can be used to upload files from the client computer to the remote server via the HTTP POST method. This variable contains parameters. The firs one being the form’s input name and the second is either [“name”], [“type”], [“size”], [“tmp_name”] or [“error”].

$_FILES[“file”][“name”] // the name of the uploaded file
$_FILES[“file”][“type”] // the type of the uploaded file
$_FILES[“file”][“size”] // the size in bytes of the uploaded file
$_FILES[“file”][“tmp_name”] // the name of the temporary copy of the file stored on the server
$_FILES[“file”][“error”] // the error code resulting from the file upload


A Cookie is often used to identify a user. With PHP you can create and store this user data into cookies by using the $_COOKIE Superglobal variable. In this example we will go over how to setup a cookie (using setcookie(name, value, expire, path, domain);) and request the data later with $_COOKIE.


The last Superglobal variable is the $_SESSION which solves the problem of the web server of knowing who you are and what you do. A PHP session allows you to store user information on the server for later use like a username or shopping cart items. Note that this is only for temporary use as the session expires once the site is exited. Here is an example.

Now we can store the session variables

Wrapping It Up

These are just the fundamentals for PHP but are necessary to grasping the full capabilities of what PHP has to offer. With practice and rigorous testing, you can create some pretty creative and unique applications.

Leave a Reply