PHP Superglobals: Global Variables

Superglobals were introduced in PHP 4.1.0. They are built-in variables that are always available in all scopes. You can access them from any function, class or file without having to do anything special.

PHP Superglobals: Global Variables

The PHP superglobal variables are:

  1. $GLOBALS - Contains all global variables, including other superglobals.

  2. $_SERVER - Contains information about the web server and the request made to it.

  3. $_REQUEST - Contains $_GET, $_POST, and possibly $_COOKIE variables.

  4. $_POST - Contains variables sent via an HTTP POST request.

  5. $_GET - Contains variables sent via an HTTP GET request.

  6. $_FILES - Contains variables sent via an HTTP POST file upload.

  7. $_ENV - Contains all environment variables set by the web server.

  8. $_COOKIE - Contains variables sent via HTTP cookies.

  9. $_SESSION - Contains variables stored in a user's session.

PHP $_SERVER Global Variable

$_SERVER is a PHP super global variable which holds information about the server and the environment. It includes headers, paths, and script locations.

You can use the print_r statement to print the contents of $_SERVER. The pre tags above and below it separates each item out on to a new line, making it easier to read.

<?php
echo '<pre>';
print_r($_SERVER);
echo '</pre>';
?> 
  • $_SERVER['SERVER_ADDR']: Returns the IP address of the host server
  • $_SERVER['SERVER_NAME']: Returns the name of the host server
  • $_SERVER['REQUEST_METHOD']: Returns the request method used to access the page
  • $_SERVER['QUERY_STRING']: Returns the query string if the page is accessed via a query string
  • $_SERVER['HTTP_HOST']: Returns the Host header from the current request
  • $_SERVER['HTTPS']: Is the script queried through a secure HTTP protocol
  • $_SERVER['REMOTE_ADDR']: Returns the IP address from where the user is viewing the current page
  • $_SERVER['REMOTE_HOST']: Returns the Host name from where the user is viewing the current page
  • $_SERVER['REMOTE_PORT']: Returns the port being used on the user's machine to communicate with the web server
  • $_SERVER['SERVER_PORT']: Returns the port on the server machine being used by the web server for communication
  • $_SERVER['SCRIPT_NAME']: Returns the path of the current script
  • $_SERVER['SCRIPT_URI']: Returns the URI of the current page