I’ve seen too many beginners get stuck on $_SERVER because they think it’s magic. It isn’t. It’s just a superglobal array PHP populates with data about the current request, your server, and the visitor.

Here is the direct answer to what you can actually use from it:

<?php
echo $_SERVER['REQUEST_METHOD']; // GET or POST
echo $_SERVER['REMOTE_ADDR'];    // Visitor IP (or proxy)
echo $_SERVER['HTTP_HOST'];      // Domain name

That’s it. You don’t need to memorize the whole list. You just need to know which keys exist and, more importantly, which ones you can trust.


What $_SERVER actually contains

The original post I wrote years ago showed a simplified dump. Here is the real deal, with descriptions that make sense.

Array (
    [HTTP_USER_AGENT] => Visitor browser information
    [SERVER_NAME] => Name of server
    [SERVER_ADDR] => Server IP address
    [SERVER_PORT] => Server port
    [REMOTE_ADDR] => Visitor's IP address
    [DOCUMENT_ROOT] => The web root folder the server serves files from
    [SCRIPT_FILENAME] => Absolute path of the running script
    [REQUEST_URI] => The URI portion of the current request
    [SCRIPT_NAME] => Name of working file
    [PHP_SELF] => Name of the working file
    [REQUEST_TIME] => Unix timestamp of the request start
)

Two things I got wrong or omitted before. SCRIPT_FILENAME is the absolute path on disk, not just a relative address. And REQUEST_TIME is a Unix timestamp, which is useful if you need to log when exactly a request hit your server.

You will also see these keys in production, and you should know what they do:

  • REQUEST_METHOD: The HTTP method, usually GET or POST. This is how you check if a form was submitted.
  • HTTP_HOST: The hostname from the request header. Use this to build URLs, not SERVER_NAME, because SERVER_NAME can be configured differently on the server side.
  • HTTPS: Set to on if the request is over HTTPS. Empty or unset otherwise.
  • QUERY_STRING: The raw query string part of the URL, without the leading ?.
  • HTTP_REFERER: The URL of the page that linked to your current page. It is optional. Browsers can strip it. Users can change it. Never rely on it for logic.

Practical snippets you will actually use

You don’t need to dump the whole array. You need to check a few specific values.

Check if it is a POST request:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // handle form submission
}

Build the current URL:

$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$currentUrl = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

This is how you get the full URL of the page the user is currently on. It works for both HTTP and HTTPS. It uses HTTP_HOST instead of SERVER_NAME because that is what the browser actually requested.

Get the visitor’s IP:

$ip = $_SERVER['REMOTE_ADDR'] ?? '';

That ?? '' part matters. If you run PHP from the command line, or in a CLI script, $_SERVER might not have all the keys you expect. Always guard against missing keys.


The security part everyone skips

Here is where I used to mess up. I treated $_SERVER like trusted data. It is not.

Everything starting with HTTP_ comes from the visitor. That includes HTTP_USER_AGENT, HTTP_REFERER, HTTP_HOST. A user can set any of these to whatever they want in their browser or with a tool like curl. You can log them. You can use them for analytics. But you should never use them for security decisions unless you have already validated them.

PHP_SELF is the sneaky one. It tells you the name of the current script, but it can contain injected code if you are not careful. If you print it into an HTML form action, wrap it in htmlspecialchars().

echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '">';

Without that function, a malicious user could craft a request that makes PHP_SELF include a script tag or an event handler. Then your form submits to JavaScript instead of your PHP file. That is a cross-site scripting vector.

REMOTE_ADDR is the address of whoever connected to your server. If you are behind a proxy, or if you use Cloudflare, that is not the real visitor IP. It is the proxy’s IP.

To get the real visitor IP, you need HTTP_X_FORWARDED_FOR. But here is the catch: any client can set that header. If you trust it blindly, an attacker can claim their IP is anything they want.

Only trust HTTP_X_FORWARDED_FOR if you know exactly which proxy added it, and you are verifying that the connection came from that specific proxy. Otherwise, use REMOTE_ADDR and accept that it might be a proxy.


Stop guessing, start checking

The biggest mistake I made was assuming $_SERVER would always have every key. It does not. On the command line, in certain SAPI configurations, or when a header is missing, keys disappear.

Always use the null coalescing operator when you read from it.

$agent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';

That one habit saves you from a Notice: Undefined index error that breaks your page in production.

$_SERVER is not magic. It is just data about the current request. Some of it is trustworthy. Some of it comes from the visitor. Know which is which, and you will stop fighting with it.