PHP $_SERVER
When you need details about the web server, such as headers, paths or where a script is located, look in the $_SERVER superglobal.
Like the other superglobals, it is built in and can be used from any scope.
Note: The web server is what fills in these entries, so one server or environment may provide an entry that another doesn't.
Here are a few of them in use:
Use $_SERVER Elements
Print several $_SERVER values, one after another:
Prompt
Write a PHP script that echoes the values of several $_SERVER elements: PHP_SELF, SERVER_NAME, HTTP_HOST, HTTP_REFERER, HTTP_USER_AGENT, and SCRIPT_NAME.
Output: PHP script
index.php
<?php
echo $_SERVER['PHP_SELF'];
echo $_SERVER['SERVER_NAME'];
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['HTTP_REFERER'];
echo $_SERVER['HTTP_USER_AGENT'];
echo $_SERVER['SCRIPT_NAME'];
?>
The $_SERVER Elements
These are the entries of $_SERVER you are most likely to need:
$_SERVER['PHP_SELF']- The filename of the script that is running right now$_SERVER['GATEWAY_INTERFACE']- The CGI (Common Gateway Interface) version in use on the server$_SERVER['SERVER_ADDR']- The host server's IP address$_SERVER['SERVER_NAME']- The host server's name (such as www.PromptCookbook.com)$_SERVER['SERVER_SOFTWARE']- The string that identifies the server software (such as Apache/2.2.24)$_SERVER['SERVER_PROTOCOL']- The information protocol's name and revision (for example HTTP/1.1)$_SERVER['REQUEST_METHOD']- The method used to request the page (for example POST)$_SERVER['REQUEST_TIME']- A timestamp of the moment the request started (for example 1377687496)$_SERVER['QUERY_STRING']- The query string, when the page was requested with one$_SERVER['HTTP_ACCEPT']- The Accept header sent with this request$_SERVER['HTTP_ACCEPT_CHARSET']- This request's Accept_Charset header (for example utf-8,ISO-8859-1)$_SERVER['HTTP_HOST']- The Host header sent with this request$_SERVER['HTTP_REFERER']- The complete URL of the page that sent the user here; you cannot rely on it, since not every user agent provides it$_SERVER['HTTPS']- Tells you whether the script was requested over secure HTTP$_SERVER['REMOTE_ADDR']- The IP address the visitor is viewing the page from$_SERVER['REMOTE_HOST']- The host name the visitor is viewing the page from$_SERVER['REMOTE_PORT']- The port the visitor's machine uses to talk to the web server$_SERVER['SCRIPT_FILENAME']- The full, absolute path of the running script$_SERVER['SERVER_ADMIN']- The value of the SERVER_ADMIN directive from the web server's configuration file or, for a script on a virtual host, the value set for that virtual host (such as someone@PromptCookbook.com)$_SERVER['SERVER_PORT']- The port on the server machine that the web server communicates through (for example 80)$_SERVER['SERVER_SIGNATURE']- The virtual host name and server version that get added to pages the server generates$_SERVER['PATH_TRANSLATED']- The path to the current script as the file system sees it$_SERVER['SCRIPT_NAME']- The current script's path$_SERVER['SCRIPT_URI']- The current page's URI