PHP: $_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:
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
- Sample output of $_SERVER taken from localhost
- PHP : $_SERVER[‘PHP_SELF’]
- PHP : $_SERVER[‘argv’]
- PHP : Super global variable: $argc
- PHP : $_SERVER[‘GATEWAY_INTERFACE’]
- PHP : $_SERVER[‘SERVER_ADDR’]
- PHP : $_SERVER[‘SERVER_NAME’]
- PHP : $_SERVER[‘SERVER_SOFTWARE’]
- PHP : $_SERVER[‘SERVER_PROTOCOL’]
- PHP : $_SERVER[‘REQUEST_METHOD’]
- PHP : $_SERVER[‘REQUEST_TIME’]
- PHP : $_SERVER[‘QUERY_STRING’]
- PHP : $_SERVER[‘HTTP_ACCEPT’]
- PHP : $_SERVER[‘HTTP_ACCEPT_CHARSET’]
- PHP : $_SERVER[‘HTTP_HOST’]
- PHP : $_SERVER[‘REMOTE_PORT’]
- PHP : $_SERVER[‘SCRIPT_FILENAME’]
- PHP : $_SERVER[‘SERVER_ADMIN’]
- PHP : $_SERVER[‘SERVER_PORT’]
- PHP : $_SERVER[‘SERVER_SIGNATURE’]
- PHP : $_SERVER[‘PATH_TRANSLATED’]
- PHP : $_SERVER[‘SCRIPT_NAME’]
- PHP : $_SERVER[‘SCRIPT_URI’]
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) "[email protected]" ["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’]
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: $_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.
If this is a file (say arguments.php) containing $_SERVER[‘argv’], and then you run the following from command line :
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.
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.
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.
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.
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’]
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’]
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’] —
PHP: $_SERVER[‘REQUEST_TIME’]
$_SERVER[‘REQUEST_TIME’] variable fetches the timestamp of the start of the request.
Following php code used $_SERVER[‘REQUEST_TIME’]
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’].
If the above php code is saved with a filename of QUERY_STRING.php and if you add ‘?tutorial=php§ion=super-globals’ (i.e. QUERY_STRING.php?tutorial=php§ion=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’]
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
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’]
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’]
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’]
https://www.w3resource.com/php/super-variables/$_SERVER-HTTP_REFERER.php
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’]
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’]
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’]
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’]
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’]
PHP: $_SERVER[‘SCRIPT_FILENAME’]
$_SERVER[‘SCRIPT_FILENAME’] fetches the absolute pathname of the currently executing script.
Following php code used $_SERVER[‘SCRIPT_FILENAME’]
https://www.w3resource.com/php/super-variables/$_SERVER-HTTP_REFERER.php
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’]
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’]
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’]
Apache/2.2.3 (CentOS) Server at www.w3resource.com Port 80
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’]
PHP: $_SERVER[‘SCRIPT_NAME’]
$_SERVER[‘SCRIPT_NAME’] contains the path of the current script.
/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.
/php/super-variables/PHP-$_SERVER-REQUEST_URI-example.php
Follow us on Facebook and Twitter for latest update.
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you’ll have to define a sorting function first:
function sortByOrder($a, $b) < return $a['order'] - $b['order']; >usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) < return $a['order'] $b['order']; >);
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero — best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) < $retval = $a['order'] $b['order']; if ($retval == 0) < $retval = $a['suborder'] $b['suborder']; if ($retval == 0) < $retval = $a['details']['subsuborder'] $b['details']['subsuborder']; > > return $retval; >);
If you need to retain key associations, use uasort() — see comparison of array sorting functions in the manual
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook