
PHP Server Variables ($_SERVER)
$_SERVER is a PHP superglobal variable that contains information about the server and the environment in which a PHP script is running. It is an associative array that can be used to access server and environment variables, such as the script's name, the current directory, the server software version, and so on. Some common elements of $_SERVER include:
- $_SERVER['PHP_SELF']: the name of the script that is currently being executed, relative to the document root.
- $_SERVER['SERVER_NAME']: the name of the server on which the script is running.
- $_SERVER['HTTP_HOST']: The host name from HTTP Host header
- $_SERVER['HTTP_USER_AGENT']: the user agent string sent by the browser, this can be used to detect the browser and version, platform and even mobile devices.
- $_SERVER['REMOTE_ADDR']: the IP address of the client connecting to the server.
- $_SERVER['REQUEST_URI']: the URI which was given in order to access this page; for instance '/index.html'.
- $_SERVER['REQUEST_METHOD']: which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.
- $_SERVER['SCRIPT_NAME']: contains the current script's path.
- $_SERVER['SCRIPT_FILENAME']: contains the full path and filename of the current script.
- $_SERVER['SERVER_PROTOCOL']: the server protocol, for example HTTP/1.1
- $_SERVER['SERVER_SOFTWARE']: the server software, for example Apache/2.4.43 (Win64) PHP/7.4.12
- $_SERVER['HTTP_REFERER']: the previous page from which the user navigated to the current page.
It should be noted that The $_SERVER superglobal depends on the web server hosting your PHP scripts, it means some of the key-value pairs may not be available or may have different value for different web servers.