PHP $_SERVER
In this page:
$_SERVER["REQUEST_METHOD"]; // GET, POST, ...
$_SERVER["REQUEST_URI"];
$_SERVER["HTTP_HOST"];
$_SERVER["REMOTE_ADDR"];
Reading Basic Request Information
$_SERVER["REQUEST_METHOD"] tells you whether the current request was a GET, POST, or another HTTP method, and $_SERVER["REQUEST_URI"] gives you the exact path (and query string) that was requested -- two of the most commonly used $_SERVER keys.
Example: Reading Basic Request Information
<?php
// Set `$_SERVER["REQUEST_METHOD"]` to "GET"
$_SERVER["REQUEST_METHOD"] = "GET";
// Set `$_SERVER["REQUEST_URI"]` to "/products?id=5"
$_SERVER["REQUEST_URI"] = "/products?id=5";
// Print `$_SERVER["REQUEST_METHOD"] . " " . $_SERVER["REQUEST_URI"]` to the output
echo $_SERVER["REQUEST_METHOD"] . " " . $_SERVER["REQUEST_URI"];
?>
Login to try C/C++/Java/PHP code in the editor
Identifying the Server and Host
$_SERVER["HTTP_HOST"] gives the domain name the visitor used to reach the site, and $_SERVER["SERVER_NAME"] gives the server's own configured name -- these are useful for building absolute URLs or detecting which domain (in a multi-domain setup) served the current request.
Example: Identifying the Server and Host
<?php
// Set `$_SERVER["HTTP_HOST"]` to "example.com"
$_SERVER["HTTP_HOST"] = "example.com";
// Set `$_SERVER["SERVER_NAME"]` to "example.com"
$_SERVER["SERVER_NAME"] = "example.com";
// Print `$_SERVER["HTTP_HOST"] . " / " . $_SERVER["SERVER_NAME"]` to the output
echo $_SERVER["HTTP_HOST"] . " / " . $_SERVER["SERVER_NAME"];
?>
Login to try C/C++/Java/PHP code in the editor
Reading the Visitor's IP Address
$_SERVER["REMOTE_ADDR"] holds the IP address the server sees the request coming from -- useful for logging, rate limiting, or geolocation lookups, though it is not a perfectly reliable way to uniquely identify a specific visitor.
Example: Reading the Visitor's IP Address
<?php
// Set `$_SERVER["REMOTE_ADDR"]` to "192.168.1.10"
$_SERVER["REMOTE_ADDR"] = "192.168.1.10";
// Print `"Request came from: " . $_SERVER["REMOTE_ADDR"]` to the output
echo "Request came from: " . $_SERVER["REMOTE_ADDR"];
?>
Login to try C/C++/Java/PHP code in the editor
Reading HTTP Headers via $_SERVER
Many HTTP request headers are exposed through $_SERVER with an HTTP_ prefix and the header name uppercased with underscores -- $_SERVER["HTTP_USER_AGENT"] holds the browser's user-agent string, and $_SERVER["HTTP_REFERER"] holds the page the visitor came from, if any.
Example: Reading HTTP Headers via $_SERVER
<?php
// Set `$_SERVER["HTTP_USER_AGENT"]` to "Mozilla/5.0"
$_SERVER["HTTP_USER_AGENT"] = "Mozilla/5.0";
// Set `$_SERVER["HTTP_REFERER"]` to "https://google.com"
$_SERVER["HTTP_REFERER"] = "https://google.com";
// Print `$_SERVER["HTTP_USER_AGENT"] . "\n" . $_SERVER["HTTP_REFERER"]` to the output
echo $_SERVER["HTTP_USER_AGENT"] . "\n" . $_SERVER["HTTP_REFERER"];
?>
Login to try C/C++/Java/PHP code in the editor
Common $_SERVER Keys Reference
Beyond the keys already covered, a handful of others come up often: SCRIPT_NAME (the currently executing script's path), DOCUMENT_ROOT (the server's web root directory), SERVER_PROTOCOL (the HTTP version used), and QUERY_STRING (everything after the ? in the URL).
Example: Common $_SERVER Keys Reference
<?php
// Set `$_SERVER["SCRIPT_NAME"]` to "/index.php"
$_SERVER["SCRIPT_NAME"] = "/index.php";
// Set `$_SERVER["DOCUMENT_ROOT"]` to "/var/www/html"
$_SERVER["DOCUMENT_ROOT"] = "/var/www/html";
// Set `$_SERVER["QUERY_STRING"]` to "id=5"
$_SERVER["QUERY_STRING"] = "id=5";
// Print `$_SERVER["SCRIPT_NAME"] . " " . $_SERVER["DOCUMENT_ROOT"] . " " . $_SERVER["QUERY_STRING"]` to the output
echo $_SERVER["SCRIPT_NAME"] . " " . $_SERVER["DOCUMENT_ROOT"] . " " . $_SERVER["QUERY_STRING"];
?>
Login to try C/C++/Java/PHP code in the editor
- Trusting $_SERVER values that originate from the client (like HTTP_USER_AGENT or HTTP_REFERER) as though they were guaranteed accurate -- browsers and clients can send whatever values they want for these headers.
- Using $_SERVER["REMOTE_ADDR"] as a foolproof way to identify a unique visitor, when proxies, shared networks, and VPNs mean many visitors can share one IP.
- Forgetting that not every $_SERVER key is guaranteed to exist on every server setup -- always check with isset() before relying on a less-common key.
- $_SERVER is a superglobal array holding information about the current request and the server environment handling it.
- Common keys include REQUEST_METHOD, REQUEST_URI, HTTP_HOST, REMOTE_ADDR, and SCRIPT_NAME.
- Client-supplied $_SERVER values (like HTTP headers) should never be trusted blindly for security-sensitive decisions.
$_SERVER has been a core PHP superglobal since PHP's earliest versions, though the exact set of available keys can vary slightly by web server software.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: