PHP Tutorial

PHP Examples

PHP Practice

PHP Superglobal - $_SERVER (With Examples)

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

PHP $_SERVER

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:

$_SERVER['key'];

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
/index.php
$_SERVER['GATEWAY_INTERFACE'] Specify CGI version
CGI/1.1
$_SERVER['SERVER_ADDR'] Specify the host server IP address
192.168.1.1
$_SERVER['SERVER_NAME'] Specify the host server name
www.includehelp.com
$_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)
HTTP/1.1
$_SERVER['REQUEST_METHOD'] Specify request method (e.g., POST)
GET
$_SERVER['REQUEST_TIME'] Specify request start timestamp
1635346425
$_SERVER['QUERY_STRING'] Specify query string if accessed via query string
page=home&category=php
$_SERVER['HTTP_ACCEPT'] Specify Accept header from the current request
text/html,application/xhtml+
xml,application/xml;q=0.9,*/*;q=0.8
$_SERVER['HTTP_ACCEPT_CHARSET'] Specify Accept_Charset header from the current request (e.g., utf-8, ISO-8859-1)
utf-8
$_SERVER['HTTP_HOST'] Specify host header from the current request
www.includehelp.com
$_SERVER['HTTP_REFERER'] Specify the complete URL of the current page (not always reliable)
https://www.includehelp.com/php
$_SERVER['HTTPS'] Specify secure HTTP protocol indicator
off
$_SERVER['REMOTE_ADDR'] Specify user's IP address viewing the current page
203.0.113.42
$_SERVER['REMOTE_HOST'] Specify the user's Hostname viewing the current page
user.includehelp.com
$_SERVER['REMOTE_PORT'] Specify the port used on the user's machine to communicate with the web server
54789
$_SERVER['SCRIPT_FILENAME'] Specify the absolute pathname of the currently executing script
/var/www/html/index.php
$_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
80
$_SERVER['SERVER_SIGNATURE'] Specify server version and virtual host name added to server-generated pages
Apache/2.4.18 (Ubuntu) Server at 
www.includehelp.com Port 80
$_SERVER['PATH_TRANSLATED'] Specify file system-based path to the current script
/var/www/html/index.php
$_SERVER['SCRIPT_NAME'] Specify the path of the current script
/index.php
$_SERVER['SCRIPT_URI'] Specify the URI of the current page
https://www.includehelp.com/index.php

Example of $_SERVER in PHP

Consider the below example of $SERVER.

PHP code to demonstrate example of $_SERVER

<?php
// 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
echo $_SERVER["REMOTE_ADDR"];
?>

Output

/CA2.php
C:/xampp/htdocs
::1
localhost
GET
1558789727
Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/74.0.3729.169 Safari/537.36
::1

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



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.