SHARE

PHP – $_SERVER SuperGlobal Variable

In PHP $_SERVER is also a superglobal variable just like $_POST , $_GET and $_GLOBALS . Superglobals are accessible from anywhere in php code . $_SERVER is also a pre-defined variable in php to get environmental information .

Environmental information is the information such as domain name, host name , system information ,browser information etc .

Syntax

$_SERVER['VARIBLE_VALUE']

Syntax of $_SERVER variable is as much as other global variable . Only capital letters are allowed in it . Variable values are also pre-defined for this variable .

Example

$_SERVER['SCRIPT_NAME']

In this example , value SCRIPT_NAME is used for get script name (/php/index.php).

Pre-defined Elements

In $_SERVER variable all values are also pre-defined and it must be written in capital letters . All of values are as follows.

  • PHP_SELF
  • SCRIPT_NAME
  • SCRIPT_URI
  • PATH_TRANSLATED
  • SERVER_SIGNATURE
  • SCRIPT_FILENAME
  • REMOTE_HOST
  • SERVER_ADDR
  • GATEWAY_INTERFACE
  • SERVER_SOFTWARE
  • SERVER_PROTOCOL
  • REQUEST_METHOD
  • REQUEST_TIME
  • QUERY_STRING
  • HTTP_ACCEPT
  • HTTP_ACCEPT_CHARSET
  • HTTP_HOST
  • HTTP_REFERER
  • REMOTE_PORT
  • HTTPS
  • SERVER_ADMIN
  • SERVER_PORT
  • SERVER_SIGNATURE

All of these elements are pre-defined in this global variable .

For example if you use this variable and want to get host name the value [‘HTTP_HOST‘] is used . And the code to access is

$_SERVER[‘HTTP_HOST’]

Example

<html>
<head></head>
<body>
<?php
echo "Host Name is ".$_SERVER['HTTP_HOST'];
?>
</body>
</html>

Result of this code isĀ 

$_SERVER
$_SERVER

Different elements are used for different purpose . In this variable ,elements are also pre defined as you read above . And one more thing is that variable name and element name must be in capital letters .

Video Tutorial