PHP Superglobal - $_SERVER (With Examples)

By Kongnyu Carine, Shahnail Khan Last updated : December 14, 2023


PHP $_SERVER is an array that contains the server-related information like headers, paths, and script locations, a web server entries this information in this array that is accessible through the $_SERVER variable. It's automatically determined by PHP based on the server configuration and the current HTTP request. This array is accessible from any part of your script, making it a convenient resource for retrieving important details.

PHP $_SERVER superglobal variable is very important and is used to get information about the server name, documents root, and many others. There are many elements available in the $_SERVER superglobal array.

Syntax of PHP $_SERVER

The general syntax for accessing elements within the $_SERVER superglobal array in PHP is:


Here, key is a placeholder for the specific element you want to access. The $_SERVER superglobal array contains various elements that provide information about the server environment, the current request, and the client. You replace key with the name of the specific element you want to retrieve information from, and you enclose the key in single quotes.

For example:

$serverName = $_SERVER['SERVER_NAME'];

In this case, $_SERVER['SERVER_NAME'] is accessing the 'SERVER_NAME' element within the $_SERVER superglobal array, which contains the name of the server host under which the current script is executing.

PHP $_SERVER Elements/Keys

Below are the keys (or, elements) that can be used with the PHP $_SERVER superglobal to access the specific PHP server information:

Element/Code Description Example
$_SERVER['PHP_SELF'] Specify the current script filename
$_SERVER['SERVER_ADDR'] Specify the host server IP address
$_SERVER['SERVER_NAME'] Specify the host server name
$_SERVER['SERVER_SOFTWARE'] Specify server identification string (e.g., Apache/2.2.24)
Apache/2.4.18 (Ubuntu)
$_SERVER['SERVER_PROTOCOL'] Specify information protocol name and revision (e.g., HTTP/1.1)
$_SERVER['REQUEST_METHOD'] Specify request method (e.g., POST)
$_SERVER['REQUEST_TIME'] Specify request start timestamp
$_SERVER['QUERY_STRING'] Specify query string if accessed via query string
$_SERVER['HTTP_ACCEPT'] Specify Accept header from the current request
$_SERVER['HTTP_ACCEPT_CHARSET'] Specify Accept_Charset header from the current request (e.g., utf-8, ISO-8859-1)
$_SERVER['HTTP_HOST'] Specify host header from the current request
$_SERVER['HTTP_REFERER'] Specify the complete URL of the current page (not always reliable)
$_SERVER['HTTPS'] Specify secure HTTP protocol indicator
$_SERVER['REMOTE_ADDR'] Specify user's IP address viewing the current page
$_SERVER['REMOTE_HOST'] Specify the user's Hostname viewing the current page
$_SERVER['REMOTE_PORT'] Specify the port used on the user's machine to communicate with the web server
$_SERVER['SCRIPT_FILENAME'] Specify the absolute pathname of the currently executing script
$_SERVER['SERVER_ADMIN'] Specify value of the SERVER_ADMIN directive in the server configuration file
[email protected]
$_SERVER['SERVER_PORT'] Specify the port on the server machine used for communication
$_SERVER['SERVER_SIGNATURE'] Specify server version and virtual host name added to server-generated pages
Apache/2.4.18 (Ubuntu) Server at Port 80
$_SERVER['PATH_TRANSLATED'] Specify file system-based path to the current script
$_SERVER['SCRIPT_NAME'] Specify the path of the current script
$_SERVER['SCRIPT_URI'] Specify the URI of the current page

Example of $_SERVER in PHP

Consider the below example of $SERVER.

PHP code to demonstrate example of $_SERVER

// file name of the present executing script
echo $_SERVER["PHP_SELF"] . "<br>";

//the document root of the server
echo $_SERVER["DOCUMENT_ROOT"] . "<br>";

//the ip address of the server
echo $_SERVER["SERVER_ADDR"] . "<br>";

//the name of the server
echo $_SERVER["SERVER_NAME"] . "<br>";

//the method the page is using to request
echo $_SERVER["REQUEST_METHOD"] . "<br>";

// the time stamp of the start of the request
echo $_SERVER["REQUEST_TIME"] . "<br>";

//used to detect the OS, browser
echo $_SERVER["HTTP_USER_AGENT"] . "<br>";

// the current user ip address


Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/74.0.3729.169 Safari/537.36

Note: The output is based on my own computer, so yours may be different.

All PHP Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.