Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

$_SERVER

$_SERVER

$_SERVER is a PHP superglobal variable that holds information about headers, paths, and script locations.

The example below demonstrates how to use some of the elements in $_SERVER:

Example

echo $_SERVER['PHP_SELF'];
echo $_SERVER['SERVER_NAME'];
echo $_SERVER['HTTP_HOST'];
echo $_SERVER['HTTP_REFERER'];
echo $_SERVER['HTTP_USER_AGENT'];
echo $_SERVER['SCRIPT_NAME'];

The table below lists the most important elements that can be found in $_SERVER:

Element/Code

Description

$_SERVER[‘PHP_SELF’]

Returns the name of the currently executing script

$_SERVER[‘GATEWAY_INTERFACE’]

Provides the version of the Common Gateway Interface (CGI) being used by the server

$_SERVER[‘SERVER_ADDR’]

Retrieves the IP address of the host server

$_SERVER[‘SERVER_NAME’]

Provides the name of the host server (e.g., www.code7school.com )

$_SERVER[‘SERVER_SOFTWARE’]

Provides the server identification string (e.g., Apache/2.2.24)

$_SERVER[‘SERVER_PROTOCOL’]

Provides the name and version of the information protocol (e.g., HTTP/1.1)

$_SERVER[‘REQUEST_METHOD’]

Returns the request method used to access the page (e.g., POST)

$_SERVER[‘REQUEST_TIME’]

Returns the timestamp indicating the start of the request (e.g., 1377687496)

$_SERVER[‘QUERY_STRING’]

Returns the query string if the page is accessed with one.

$_SERVER[‘HTTP_ACCEPT’]

Provides the Accept header from the current request.

$_SERVER[‘HTTP_ACCEPT_CHARSET’]

Returns the Accept-Charset header from the current request (e.g., utf-8, ISO-8859-1).

$_SERVER[‘HTTP_HOST’]

Returns the Host header from the current request.

$_SERVER[‘HTTP_REFERER’]

Returns the complete URL of the current page (though it may not be reliable as not all user agents support this).

$_SERVER[‘HTTPS’]

Is the script accessed via a secure HTTP protocol

$_SERVER[‘REMOTE_ADDR’]

Returns the IP address from which the user is accessing the current page.

$_SERVER[‘REMOTE_HOST’]

Returns the hostname from which the user is accessing the current page.

$_SERVER[‘REMOTE_PORT’]

Returns the port being used on the user’s machine to connect to the web server.

$_SERVER[‘SCRIPT_FILENAME’]

Returns the absolute path of the currently executing script.

$_SERVER[‘SERVER_ADMIN’]

Returns the value assigned to the SERVER_ADMIN directive in the web server configuration file. If your script runs on a virtual host, it will reflect the value defined for that specific virtual host

$_SERVER[‘SERVER_PORT’]

Returns the port on the server being used by the web server for communication (e.g., 80).

$_SERVER[‘SERVER_SIGNATURE’]

Returns the server version and virtual host name that are included in server-generated pages.

$_SERVER[‘PATH_TRANSLATED’]

Returns the file system path to the current script.

$_SERVER[‘SCRIPT_NAME’]

Returns the path of the current script.

$_SERVER[‘SCRIPT_URI’]

Returns the URI of the current page.