PHP Superglobals

The PHP super global variable is used to access global variables from anywhere in PHP script. These are built-in variables that are always available in all scopes. Super Global variables are an array variable in PHP. List of PHP superglobal variables are given below:

$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

PHP $GLOBALS variable :

$GLOBALS is a php global variable which contains all global variables in php script, including other superglobals. PHP stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.

Example :
<?php
  $a = 5;
  $b = 8;

  function multiply() {
    $GLOBALS['ans'] = $GLOBALS['a'] * $GLOBALS['b'];
  }

  multiply();
  echo $ans;

?>
Output :

40


PHP $_SERVER variable :

$_SERVER is a PHP super global variable which holds information about headers, paths, and script locations. The entries in these array are created by web servers.

Example :
<?php
 
  echo $_SERVER['PHP_SELF'];
  echo "<br>";
  echo $_SERVER['SERVER_NAME'];
  echo "<br>";
  echo $_SERVER['HTTP_HOST'];
  echo "<br>";
  echo $_SERVER['HTTP_REFERER'];
  echo "<br>";
  echo $_SERVER['HTTP_USER_AGENT'];
  echo "<br>";
  echo $_SERVER['SCRIPT_NAME'];

?>
Output :

/test.php
localhost
localhost:8000

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
/test.php

Some of the most important elements of $_SERVER super global variable is as follows :

Element Description
$_SERVER['PHP_SELF'] Returns the filename of the currently executing script.
$_SERVER['GATEWAY_INTERFACE'] Returns the version of the Common Gateway Interface (CGI) the server is using.
$_SERVER['SERVER_ADDR'] Returns the IP address of the host server.
$_SERVER['SERVER_NAME'] Returns the name of the host server.
$_SERVER['SERVER_SOFTWARE'] Returns the server identification string (such as Apache/2.2.24).
$_SERVER['SERVER_PROTOCOL'] Returns the name and revision of the information protocol (such as HTTP/1.1).
$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (such as POST).
$_SERVER['REQUEST_TIME'] Returns the timestamp of the start of the request (such as 1377687496).
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string.
$_SERVER['HTTP_ACCEPT'] Returns the Accept header from the current request.
$_SERVER['HTTP_ACCEPT_CHARSET'] Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1).
$_SERVER['HTTP_HOST'] Returns the Host header from the current request.
$_SERVER['HTTP_REFERER'] Returns the complete URL of the current page (not reliable because not all user-agents support it).
$_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['SCRIPT_FILENAME'] Returns the absolute pathname of the currently executing script.
$_SERVER['SERVER_ADMIN'] Returns the value given to the SERVER_ADMIN directive in the web server configuration file.
$_SERVER['SERVER_PORT'] Returns the port on the server machine being used by the web server for communication (such as 80).
$_SERVER['SERVER_SIGNATURE'] Returns the server version and virtual host name which are added to server-generated pages.
$_SERVER['PATH_TRANSLATED'] Returns the file system based path to the current script.
$_SERVER['SCRIPT_NAME'] Returns the path of the current script.
$_SERVER['SCRIPT_URI'] Returns the URI of the current page.

PHP $_REQUEST variable :

The $_REQUEST is used to access the data after submission of HTML form. The form method could be 'GET' or 'POST'. For example :
<html>
<body>

  <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get" >
    Product Name: <input type="text" name="username">
    <input type="submit">
  </form>

<?php

  if ($_SERVER["REQUEST_METHOD"] == "GET") {
    if ($_REQUEST['username']) {
      echo "Hello " . $_REQUEST['username'];
    } else {
      echo "hello Guest.";
    }
  }

?>

</body>
</html>
Output :


PHP $_POST and $_GET :

PHP $_POST and $_GET are widely used to collect form data after submitting an HTML form. If the farm will submitted with the $_POST method, then we need to use $_POST global variable, For example :
<html>
<body>

  <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" >
    Product Name: <input type="text" name="username">
    <input type="submit">
  </form>

<?php

  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_REQUEST['username'];
    if (empty($name)) {
      echo "hello Guest.";
    } else {
      echo "Hello " . $name;
    }
  }

?>

</body>
</html>
Output :


Similarly the $_GET can also be used to collect form data after submitting an HTML form with method="get". $_GET can also collect data sent in the URL. For example :
<?php

  echo "Name : " . $_GET['name'] . ", Age : " . $_GET['age'];

?>
When we put the name and age on the url like below example,then the page will print the output :
 http://localhost/test.php?name=John&age=40
Output :



Next Topic :