w3resource

PHP: $_SERVER

$_SERVER

$_SERVER is an array which holds information of headers, paths, script locations. Web server creates the entries in the array. This is not assured that every web server will provide similar information, rather some servers may include or exclude some information which are not listed here.

$_SERVER has following basic properties:

1. Set by web server.

2. Directly related to the runtime environment of the current php script.

3. It does the same job as $HTTP_SERVER_VARS used to do in previous versions of PHP

Contents:

 

Sample output of $_SERVER taken from localhost

array(32) {
["HTTP_HOST"]=>
string(9) "localhost"
["HTTP_CONNECTION"]=>
string(10) "keep-alive"
["HTTP_USER_AGENT"]=>
string(101) "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11
(KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11"
["HTTP_ACCEPT"]=>
string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
["HTTP_REFERER"]=>
string(49) "http://localhost/php/super-variables/$_SERVER.php"
["HTTP_ACCEPT_ENCODING"]=>
string(17) "gzip,deflate,sdch"
["HTTP_ACCEPT_LANGUAGE"]=>
string(14) "en-US,en;q=0.8"
["HTTP_ACCEPT_CHARSET"]=>
string(30) "ISO-8859-1,utf-8;q=0.7,*;q=0.3"
["HTTP_COOKIE"]=>
string(52) "bsau=13422601771944046296; bsas=13422601771975238542"
["PATH"]=>
string(120) "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\; c:\python32\python;"
["SystemRoot"]=>
string(10) "C:\Windows"
["COMSPEC"]=>
string(27) "C:\Windows\system32\cmd.exe"
["PATHEXT"]=>
string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
["WINDIR"]=>
string(10) "C:\Windows"
["SERVER_SIGNATURE"]=>
string(0) ""
["SERVER_SOFTWARE"]=>
string(31) "Apache/2.2.11 (Win32) PHP/5.3.0"
["SERVER_NAME"]=>
string(9) "localhost"
["SERVER_ADDR"]=>
string(9) "127.0.0.1"
["SERVER_PORT"]=>
string(2) "80"
["REMOTE_ADDR"]=>
string(9) "127.0.0.1"
["DOCUMENT_ROOT"]=>
string(12) "F:/wamp/www/"
["SERVER_ADMIN"]=>
string(15) "admin@localhost"
["SCRIPT_FILENAME"]=>
string(49) "F:/wamp/www/php/super-variables/test-$_server.php"
["REMOTE_PORT"]=>
string(5) "51124"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["REQUEST_METHOD"]=>
string(3) "GET"
["QUERY_STRING"]=>
string(0) ""
["REQUEST_URI"]=>
string(38) "/php/super-variables/test-$_server.php"
["SCRIPT_NAME"]=>
string(38) "/php/super-variables/test-$_server.php"
["PHP_SELF"]=>
string(38) "/php/super-variables/test-$_server.php"
["REQUEST_TIME"]=>
int(1342260551)
}

Below we have discussed the elements which can exist within $_SERVER.

PHP: $_SERVER['PHP_SELF']

You can find the filename of the currently executing script by using $_SERVER['PHP_SELF']. Filename shown as output is relative to the root of the document.

Following php code used $_SERVER['PHP_SELF']

<?php
echo $_SERVER['PHP_SELF'];
?>

View the example in browser

Another advanced example:

Basic knowledge of array is a prerequisite of this example. You can get back to this example after you learn php array. The following PHP code compares the array elements with the $_SERVER['PHP_SELF'] and displays a message.

<?php
$findit=array('/php/super-variables/test.php',
'/php/super-variables/test1123.php',
'/php/super-variables/php-self-advanced-example1.php'
);
for ($j=0; $j<count($findit); $j++)
{
if ($_SERVER['PHP_SELF']==$findit[$j])
echo "You are learning PHP Super Globals";
}
?>

View the example in browser

PHP: $_SERVER['argv']

If used, all the arguments passed from command line, are stored in $_SERVER['argv'] array. The file name itself is the first item in the array, i.e. 0.

Example:

<?php
echo $_SERVER['argv'];
?>

If this is a file (say arguments.php)  containing $_SERVER['argv'], and then you run the following from command line : 

D:\php\php.exe d:\arguments.php w3resource . com

where php is installed in D:\php and arguments.php is saved in D:\

Output of the above command is :

d:\arguments.php
w3resource
.
com

 

PHP: Super global variable: $argc

$argc can contain a number of command line parameters. These parameters are passed to the corresponding script.

Example:

<?php
var_dump($argc);
?>

If the code above is saved as argc.php and executed from command prompt with arguments arg1, arg2 and arg3; here is the output -

C:\wamp\bin\php\php5.3.0>php.exe-f "C:\argc.php" -- -arg1 -arg2 -arg3 int(4)
C:\wamp\bin\php\php5.3.0>

PHP: $_SERVER['GATEWAY_INTERFACE']

$_SERVER['GATEWAY_INTERFACE'] variable returns the version of the Common Gateway Interface (CGI) specification the server is using. For example CGI/1.1 is a valid GATEWAY_INTERFACE.

Following php code used $_SERVER['GATEWAY_INTERFACE']. Notice one point, if any server does not contain Common Gateway Interface (CGI), it won't display anything.

Example:

<?php
echo $_SERVER['GATEWAY_INTERFACE'];
?>

PHP: $_SERVER['SERVER_ADDR']

The $_SERVER['SERVER_ADDR'] returns the IP address (Internet Protocol address) of the host server.

Following php code used $_SERVER['SERVER_ADDR'] to display the IP address of the host server.

Example:

<?php
echo $_SERVER['SERVER_ADDR'];
?>

Sample output :

xxx.xx.xxx.xx

Note : If you run the above code on localhost it will return 127.0.0.1

PHP: $_SERVER['SERVER_NAME']

States name of the host server.

Following php code used $_SERVER['SERVER_NAME'] variable to display name of the host server.

Example:

<?php
echo $_SERVER['SERVER_NAME'];
?>

Output:

www.w3resource.com

View the example in browser

Note : If you run the above code on localhost, by default it will show the server name as localhost. If the script is running on a virtual host, the name of the virtual host set, will be returned as server name.

PHP: $_SERVER['SERVER_SOFTWARE']

$_SERVER['SERVER_SOFTWARE'] variable fetches the server identification string. The string is provided in the headers when responding to requests.

Following php code used $_SERVER['SERVER_SOFTWARE']

Example:

<?php
echo $_SERVER['SERVER_SOFTWARE'];
?>

Output:

Apache/2.2.3 (CentOS)

View the example in browser

PHP: $_SERVER['SERVER_PROTOCOL']

$_SERVER['SERVER_PROTOCOL'] variable fetches the name and revision of the information protocol via which the page has been requested.

Following php code used $_SERVER['SERVER_PROTOCOL']

Example:

<?php
echo $_SERVER['SERVER_PROTOCOL'];
?>

Output :

HTTP/1.1

View the example in browser

PHP: $_SERVER['REQUEST_METHOD']

$_SERVER['REQUEST_METHOD'] fetches the request method used to access the page. Request methods are 'GET', 'HEAD', 'POST', 'PUT'.

Following php code used $_SERVER['REQUEST_METHOD'] -

Example:

<?php
echo $_SERVER['REQUEST_METHOD'];
?>

Output:

GET

View the example in browser

PHP: $_SERVER['REQUEST_TIME']

$_SERVER['REQUEST_TIME'] variable fetches the timestamp of the start of the request.

Following php code used $_SERVER['REQUEST_TIME']

Example:

<?php
echo $_SERVER['REQUEST_TIME'];
?>

View the example in browser

PHP: $_SERVER['QUERY_STRING']

If a page is accessed via any query string, $_SERVER['QUERY_STRING'] fetches that query string.

Following php code used $_SERVER['QUERY_STRING'].

Example:

<?php
echo "The query string is: ".$_SERVER['QUERY_STRING'];
?>

If the above php code is saved with a filename of QUERY_STRING.php and if you add '?tutorial=php&section=super-globals' (i.e. QUERY_STRING.php?tutorial=php&section=super-globals); it will print this string in the page since you have asked the script to print $_SERVER['QUERY_STRING'].

 

PHP: $_SERVER['HTTP_ACCEPT']

If exists, contents of the Accept: header from the current request is fetched by $_SERVER['HTTP_ACCEPT'].

Following php code used $_SERVER['HTTP_ACCEPT']

Example:

<?php
echo $_SERVER['HTTP_ACCEPT'];
?>

Output:

text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

View the example in browser

PHP: $_SERVER['HTTP_ACCEPT_CHARSET']

If exists, contents of the Accept-Charset: header from the current request is fetched by $_SERVER['HTTP_ACCEPT_CHARSET'].

Following php code used $_SERVER['HTTP_ACCEPT_CHARSET']

Example:

<?php
echo $_SERVER['HTTP_ACCEPT_CHARSET'];
?>

Output:

ISO-8859-1,utf-8;q=0.7,*;q=0.7

View the example in browser

PHP: $_SERVER['HTTP_HOST']

States name of the host server Contents of the Host: header from the current request, if there is one.

Following php code used $_SERVER['HTTP_HOST']

Example :

<?php
echo $_SERVER['HTTP_HOST'];
?>

Output:

www.w3resource.com

View the example in browser

PHP: $_SERVER['HTTP_REFERER']

$_SERVER['HTTP_REFERER'] can be used to fetch the complete URL of the current page.

Since some but not all user agents (for example browsers) don't support it, usage of this is not much reliable.

Following php code used $_SERVER['HTTP_REFERER']

Example:

<?php
echo $_SERVER['HTTP_REFERER'];
?>

Output:

https://www.w3resource.com/php/super-variables/$_SERVER-HTTP_REFERER.php

View the example in browser

PHP: $_SERVER['HTTP_USER_AGENT']

If exists, contents of the User-Agent: header from the current request is fetched by $_SERVER['HTTP_USER_AGENT'].

Following php code used $_SERVER['HTTP_USER_AGENT']

Example:

<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

View the example in browser

PHP: $_SERVER['HTTPS']

If the script was queried through a secure HTTP protocol, $_SERVER['HTTPS'] is set to a non-empty value.

Following php code used $_SERVER['HTTPS']

Example:

<?php
echo $_SERVER['HTTPS'];
?>

PHP: $_SERVER['REMOTE_ADDR']

$_SERVER['REMOTE_ADDR'] fetches the IP address from which the user is viewing the current page.

Following php code used $_SERVER['REMOTE_ADDR']

Example:

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

View the example in browser

PHP : $_SERVER['REMOTE_HOST']

$_SERVER['REMOTE_HOST'] fetches the Host name from which the user is viewing the current page.

But for this script to work, Hostname Lookups On inside httpd.conf must be configured.

Following php code used $_SERVER['REMOTE_HOST']

Example:

<?php
echo $_SERVER['REMOTE_HOST'];
?>

PHP: $_SERVER['REMOTE_PORT']

States name of the host server. The port being used on the user's machine to communicate with the web server.

Following php code used $_SERVER['REMOTE_PORT']

Example:

<?php
echo $_SERVER['REMOTE_PORT'];
?>

View the example in browser

PHP: $_SERVER['SCRIPT_FILENAME']

$_SERVER['SCRIPT_FILENAME'] fetches the absolute pathname of the currently executing script.

Following php code used $_SERVER['SCRIPT_FILENAME']

Example:

<?php
echo $_SERVER['SCRIPT_FILENAME'];
?>

Output:

https://www.w3resource.com/php/super-variables/$_SERVER-HTTP_REFERER.php

View the example in browser

 

PHP: $_SERVER['SERVER_ADMIN']

$_SERVER-SERVER_ADMIN.php fetches the value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file.

If the script is running on a virtual host, this will be the value defined for that virtual host.

Following php code used $_SERVER['SERVER_ADMIN']

Example:

<?php
echo $_SERVER['SERVER_ADMIN'];
?>

Output:

[email protected]

View the example in browser

PHP $_SERVER['SERVER_PORT']

States name of the host server The port on the server machine being used by the web server for communication. For default setups, this will be '80'; using SSL, for instance, will change this to whatever your defined secure HTTP port is.

Following php code used $_SERVER['SERVER_PORT']

Example:

<?php
echo $_SERVER['SERVER_PORT'];
?>

View the example in browser

 

PHP : $_SERVER['SERVER_SIGNATURE']

$_SERVER['SERVER_SIGNATURE'] fetches the string containing the server version and virtual host name which are added to server-generated pages, if enabled.

Following php code used $_SERVER['SERVER_SIGNATURE']

Example:

<?php
echo $_SERVER['SERVER_SIGNATURE'];
?>

Output:

Apache/2.2.3 (CentOS) Server at www.w3resource.com Port 80

View the example in browser

PHP: $_SERVER['PATH_TRANSLATED']

$_SERVER['PATH_TRANSLATED'] fetches the file system based path to the current script. This is accomplished after the server has done any virtual-to-real mapping.

It works only when PATH_INFO is defined.

Following php code used $_SERVER['PATH_TRANSLATED']

Example:

<?php
echo $_SERVER['PATH_TRANSLATED'];
?>

PHP: $_SERVER['SCRIPT_NAME']

$_SERVER['SCRIPT_NAME'] contains the path of the current script.

Example:

<?php
echo $_SERVER['SCRIPT_NAME'];
?>

Output:

/php/super-variables/$_SERVER-SERVER_SCRIPT_NAME-example1.php

PHP: $_SERVER['SCRIPT_URI']

$_SERVER['REQUEST_URI'] contains the URI of the current page.

So if the full path of a page is https://www.w3resource.com/html/html-tutorials.php, $_SERVER['REQUEST_URI'] would contain /html/html-tutorials.php.

Following php code used $_SERVER['REQUEST_URI'] variable.

Example:

<?php
echo $_SERVER['REQUEST_URI'];
?>

Output:

/php/super-variables/PHP-$_SERVER-REQUEST_URI-example.php

View the example in browser

Previous: $GLOBALS
Next: $_REQUEST, $_POST, $_GET



Follow us on Facebook and Twitter for latest update.