Introduction
$_SERVER is a superglobal that holds information regarding HTTP headers, path and script location etc. All the server and execution environment related information is available in this associative array. Most of the entries in this array are populated by web server.
PHP versions prior to 5.4.0 contained $HTTP_SERVER_VARS contained same information but has now been removed. Following are some prominent members of this array
PHP_SELF − stores filename of currently executing script. For example, a script in test folder of document root of a local server returns its path as follows −
Example
<?php echo $_SERVER['PHP_SELF']; ?>
This results in following output in browser with https://localhost/test/testscript.php URL
/test/testscript.php
SERVER_ADDR − This property of array returns The IP address of the server under which the current script is executing.
SERVER_NAME − Name of server hostunder which the current script is executing.In case of a erver running locally, localhost is returned
QUERY_STRING − A query string is the string of key=value pairs separated by & symbol and appended to URL after ? symbol. For example, https://localhost/testscript?name=xyz&age=20 URL returns trailing query string
REQUEST_METHOD − HTTP request method used for accessing a URL, such as POST, GET, POST, PUT or DELETE. In above query string example, a URL attached to query string wirh ? symbol requests the page with GET method
DOCUMENT_ROOT − returns name of directory on server that is configured as document root. On XAMPP apache server it returns htdocs as name of document root
C:/xampp/htdocs
DOCUMENT_ROOT − This is a string denoting the user agent (browser) being which is accessing the page.
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
REMOTE_ADDR − IP address of machine from which the user is viewing the current page.
SERVER_PORT − port number on which the web server is listening to incoming request. Default is 80
Following script invoked from document root of XAMPP server lists all server variables
Example
<?php foreach ($_SERVER as $k=>$v) echo $k . "=>" . $v . "<br>"; ?>
List of all server variables
MIBDIRS=>C:/xampp/php/extras/mibs MYSQL_HOME=>\xampp\mysql\bin OPENSSL_CONF=>C:/xampp/apache/bin/openssl.cnf PHP_PEAR_SYSCONF_DIR=>\xampp\php PHPRC=>\xampp\php TMP=>\xampp\tmp HTTP_HOST=>localhost HTTP_CONNECTION=>keep-alive HTTP_CACHE_CONTROL=>max-age=0 HTTP_DNT=>1 HTTP_UPGRADE_INSECURE_REQUESTS=>1 HTTP_USER_AGENT=>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36 HTTP_ACCEPT=>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 HTTP_SEC_FETCH_SITE=>none HTTP_SEC_FETCH_MODE=>navigate HTTP_SEC_FETCH_USER=>?1 HTTP_SEC_FETCH_DEST=>document HTTP_ACCEPT_ENCODING=>gzip, deflate, br HTTP_ACCEPT_LANGUAGE=>en-US,en;q=0.9,mr;q=0.8 PATH=>C:\python37\Scripts\;C:\python37\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\AMD\ATI.ACE\Core-Static;C:\python37\Scripts\;C:\python37\;C:\Users\User\AppData\Local\Microsoft\WindowsApps;C:\Users\User\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\;C:\MicrosoftVSCode\bin SystemRoot=>C:\Windows COMSPEC=>C:\Windows\system32\cmd.exe PATHEXT=>.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW WINDIR=>C:\Windows SERVER_SIGNATURE=> Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32 Server at localhost Port 80 SERVER_SOFTWARE=>Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32 SERVER_NAME=>localhost SERVER_ADDR=>::1 SERVER_PORT=>80 REMOTE_ADDR=>::1 DOCUMENT_ROOT=>C:/xampp/htdocs REQUEST_SCHEME=>http CONTEXT_PREFIX=> CONTEXT_DOCUMENT_ROOT=>C:/xampp/htdocs SERVER_ADMIN=>postmaster@localhost SCRIPT_FILENAME=>C:/xampp/htdocs/testscript.php REMOTE_PORT=>49475 GATEWAY_INTERFACE=>CGI/1.1 SERVER_PROTOCOL=>HTTP/1.1 REQUEST_METHOD=>GET QUERY_STRING=> REQUEST_URI=>/testscript.php SCRIPT_NAME=>/testscript.php PHP_SELF=>/testscript.php REQUEST_TIME_FLOAT=>1599118525.327 REQUEST_TIME=>1599118525