$_SERVER
The $_SERVER superglobal is a built-in PHP array that contains information about the current HTTP request and the server environment. Like all superglobals, it is always available in every scope, including inside functions.
The values inside $_SERVER are provided by the web server (such as Apache or Nginx), not by PHP itself. Because of this, not every key is guaranteed to exist on every server or environment. Local, staging, and production servers may expose different values.
$_SERVER includes several categories of data:
- Request information, such as the HTTP method used (
GETorPOST), the query string, and whether the request was made over HTTPS. - Script and execution context, such as the filename, path, and URL of the currently executing script.
- Server details, including server name, protocol, software, and ports.
- Client-related data, such as IP address and user agent (with some values being unreliable, like
HTTP_REFERER).
Commonly used $_SERVER entries include:
$_SERVER['REQUEST_METHOD']– how the page was accessed (GET or POST)$_SERVER['SCRIPT_NAME']/$_SERVER['PHP_SELF']– the current script path$_SERVER['SCRIPT_FILENAME']– the full filesystem path of the script$_SERVER['HTTPS']– whether the request is secure$_SERVER['REMOTE_ADDR']– the visitor’s IP address
In practice, $_SERVER represents the server’s view of the current request. Many frameworks and WordPress functions rely on this data internally, so understanding $_SERVER helps explain where request and environment information comes from, even when you don’t access it directly.