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.
This is one of the most used super global variable in PHP. This variable itself consists of several values in an array form.
and see all the current possible values in $_SERVER. The information might be a bit overwhelming.
Here are 11 (not all) useful $_SERVER variables
- $_SERVER[‘PHP_SELF’]: Current executing script like /test.php.
- $_SERVER[‘SERVER_ADDR’]: The ip address of your php server like 188.8.131.52
- $_SERVER[‘SERVER_NAME’]: The name of your local server or your domain like yourdomain.com.
- $_SERVER[‘REQUEST_TIME’]: The time stamp of the request like 1414491097.
- $_SERVER[‘QUERY_STRING’]: The query string for the page request like page=1&login=true
- $_SERVER[‘DOCUMENT_ROOT’]: The document root of the current script like /home/server/public_html/domain
- $_SERVER[‘HTTP_HOST’]: Your current host like yourdomain.com or localhost.
- $_SERVER[‘HTTP_REFERER’]: The referer or the previous URL from which you reached the current page.
- $_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
- $_SERVER[‘REMOTE_ADDR’]: Get IP address of the user/visitor. Eg. 184.108.40.206
- $_SERVER[‘SERVER_PORT’]: The port used to access the script. Usually its 80.
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