Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
superglobal
web development

What is $_SERVER in PHP?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

$_SERVER

$_SERVER is a superglobal variable in PHP. Superglobal variables are predefined variables in PHP that do not need to be declared by the user.

$_SERVER contains data such as headers, paths, and script locations. As an associative array, it has a few key-value pairs. The presence of some elements may vary from server to server as they are made by the server.

Usage

The following demonstrates how $_SERVER can be used:

<?php
// Contains file directory
echo $_SERVER['PHP_SELF'];
echo '<br>';
// Contains file directory
echo $_SERVER['SCRIPT_NAME'];
?>

The program above displays two $_SERVER elements. Both elements contain the file directory. For security reasons, the server has not filled out other elements. However, some others include SERVER_ADDRcontains the server’s IP address, REQUEST_METHODcontains the request used to access the page, and REMOTE_ADDRthe IP address from which the user is accessing the page.

The following table contains a data list that might be stored in the $_SERVER variable.

Code Description
$_SERVER['PHP_SELF'] Contains the file name of the currently running script.
$_SERVER['GATEWAY_INTERFACE'] Contains the version of the Common Gateway Interface being used by the server.
$_SERVER['SERVER_ADDR'] Contains the server's IP address.
$_SERVER['SERVER_NAME'] Contains the host name of the server.
$_SERVER['SERVER_SOFTWARE'] Contains the server identification string.
$_SERVER['SERVER_PROTOCOL'] Contains the name and revision of the version protocol.
$_SERVER['REQUEST_METHOD'] Contains the request method used to access the page.
$_SERVER['REQUEST_TIME'] Contains the timestamp of the start of the request.
$_SERVER['QUERY_STRING'] Contains the query string if the page was accessed using a query.
$_SERVER['HTTP_ACCEPT'] Contains the Accept header from the current request.
$_SERVER['HTTP_ACCEPT_CHARSET'] Contains the Accept_charset header from the current request.
$_SERVER['HTTP_HOST'] Contains the Host header from the current request.
$_SERVER['HTTP_REFERER'] Contains the URL of the current page.
$_SERVER['HTTPS'] Contains information regarding if the script was queried using a secure protocol.
$_SERVER['REMOTE_ADDR'] Contains the IP address from which the user is accessing the current page.
$_SERVER['REMOTE_HOST'] Contains the host name from where the user is accessing the current page.
$_SERVER['SCRIPT_FILENAME'] Contains the path of the script that's currently running.
$_SERVER['SERVER_ADMIN'] Contains the value given to SERVER_ADMIN in the configuration file.
$_SERVER['SERVER_PORT'] Contains the port on the web server being used for communication.
$_SERVER['SERVER_SIGNATURE'] Contains the server version and virtual host name that are added to server-generated pages.
$_SERVER['PATH_TRANSLATED'] Contains the file-system path to the current script.
$_SERVER['SCRIPT_NAME'] Contains the path to the current script.
$_SERVER['SCRIPT_URI'] Contains the URL of the current script.

RELATED TAGS

php
superglobal
web development
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring