PHP Super GLobal Variables like $_SERVER


In PHP there exists what is known as super global variables. These variables are usable from anywhere or any php file or script. They do not need to be pre-defined. Their values also need not be specified. They are used to hold certain values such as ip addresses, domain names, referrers etc. These superglobals are associative arrays.

$_SERVER

This is one of the most used super global variable in PHP. This variable itself consists of several values in an array form.

Try:
<?php
echo print_r($_SERVER);
?>
and see all the current possible values in $_SERVER. The information might be a bit overwhelming.

Here are 11 (not all) useful $_SERVER variables

  1. $_SERVER[‘PHP_SELF’]: Current executing script like /test.php.
  2. $_SERVER[‘SERVER_ADDR’]: The ip address of your php server like 74.220.215.220
  3. $_SERVER[‘SERVER_NAME’]: The name of your local server or your domain like yourdomain.com.
  4. $_SERVER[‘REQUEST_TIME’]: The time stamp of the request like 1414491097.
  5. $_SERVER[‘QUERY_STRING’]: The query string for the page request like page=1&login=true
  6. $_SERVER[‘DOCUMENT_ROOT’]: The document root of the current script like /home/server/public_html/domain
  7. $_SERVER[‘HTTP_HOST’]: Your current host like yourdomain.com or localhost.
  8. $_SERVER[‘HTTP_REFERER’]: The referer or the previous URL from which you reached the current page.
  9. $_SERVER[‘HTTP_USER_AGENT’]: Get users’ browser and other info like: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
  10. $_SERVER[‘REMOTE_ADDR’]: Get IP address of the user/visitor. Eg. 113.199.187.2
  11. $_SERVER[‘SERVER_PORT’]: The port used to access the script. Usually its 80.

Click here for the full list of $_SERVER superglobals.

There are other super globals as well. They are:
$_GET: To access the variables that are passed using GET.
$_POST: To access the variables that are passed using POST.
$_FILES: To access the items that have been uploaded.
$_COOKIE: To access the cookies that have been set.
$_SESSION: To acess the sessions that have been started.
$_REQUEST: To access contents of $_GET, $_POST as well as $_COOKIE
$_ENV: To access the variables passed using environment method

You may also like

Comments are closed here.

Advertisements

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers