PHP Server Array Variables Tutorial with Sample Test Script


This is a very quick tutorial to give you an overview of what will be the output of the most common PHP Server Variables. Please bookmark or link to this post so that you can get a very quick reference of what PHP will give as a result if those variables are used.

I upload a PHP script to this URL: http://www.php-developer.org/wp-content/uploads/scripts/PHP-Server-Array-Variables.php to let you know what are the “OUTPUTS” of those most commonly used server variables in PHP using this server/website. The source code of that script is shown below:



The PHP Source Code:

<html>
<head>
<title>Most Commonly Used Server Array Variables in PHP</title>
<meta name="ROBOTS" content="NOINDEX">
<style type="text/css">
body {
font-family:'Verdana';
font-size:12px
}
</style>
</head>
<body>
<h3>Below are the most commonly used server array variables in PHP</h3><br />
Overview: This is just an outputted HTML of the PHP server side script that utilizes the server variables below.<br />
If you like to know the details, this is what happens: <br />
Step 1. You click the link on <br />
Step 2. The server receives that request and executes the PHP code that includes the server variables.<br />
If you need to see the exact PHP script, visit this post:<br />
Step 3. After executing the PHP code, it will then output the result as PLAIN HTML CODE TO THE BROWSER.<br />
Step 4. The browser receives the request in HTML form and renders it so that you can SEE and READ the content.<br />
Enough of the background, lets study how PHP respond the server variables below (in red font are the server variable OUTPUTS):<br />
<p><font color="blue">Server variable 1:</font><font color="green"> $_SERVER['REQUEST_URI']</font></p>
<?php echo "Server REQUEST URI value of this URL/Server is:  ".'<font color="red">'.$_SERVER['REQUEST_URI'].'</font><br />'; ?>
Note: This the requested file name/URL to the server. You can use this variable to identify the requested URL.
<p><font color="blue">Server variable 2:</font><font color="green"> $_SERVER['HTTP_HOST']</font></p>
<?php echo "Server HTTP HOST value of this URL/Server is:  ".'<font color="red">'.$_SERVER['HTTP_HOST'].'</font><br />'; ?>
Note: This the server hostname. This is used to get the hostname.
<p><font color="blue">Server variable 3:</font><font color="green"> $_SERVER['DOCUMENT_ROOT']</font><p>
<?php echo "Server DOCUMENT ROOT value of this URL/Server is:  ".'<font color="red">'.$_SERVER['DOCUMENT_ROOT'].'</font><br />'; ?>
Note: This is the file path of the root directory.
<p><font color="blue">Server variable 4:</font><font color="green"> $_SERVER['SERVER_NAME']</font><p>
<?php echo "Server SERVER NAME value of this URL/Server is:  ".'<font color="red">'.$_SERVER['SERVER_NAME'].'</font><br />'; ?>
Note: This is the server name, analogous to the domain name of the requested URL.
<p><font color="blue">Server variable 5:</font><font color="green"> $_SERVER['HTTP_REFERER']</font><p>
<?php echo "Server HTTP REFERER value of this URL/Server is:  ".'<font color="red">'.$_SERVER['HTTP_REFERER'].'</font><br />'; ?>
Note: This gives the referer URL to this page
<p><font color="blue">Server variable 6:</font><font color="green"> $_SERVER['HTTP_USER_AGENT']</font><p>
<?php echo "Server HTTP USER AGENT value of this URL/Server is:  ".'<font color="red">'.$_SERVER['HTTP_USER_AGENT'].'</font><br />'; ?>
Note: This gives the exact name of the browser you are using.
<p><font color="blue">Server variable 7:</font><font color="green"> $_SERVER['SERVER_PROTOCOL']</font><p>
<?php echo "Server HTTP SERVER PROTOCOL value of this URL/Server is:  ".'<font color="red">'.$_SERVER['SERVER_PROTOCOL'].'</font><br />'; ?>
Note: This is the HTTP protocol version.
<p><font color="blue">Server variable 8:</font><font color="green"> $_SERVER['PHP_SELF']</font><p>
<?php echo "Server HTTP PHP SELF value of this URL/Server is:  ".'<font color="red">'.$_SERVER['PHP_SELF'].'</font><br />'; ?>
Note: This is used in submitting form data to itself, eliminating the need of another files.
<p><font color="blue">Server variable 9:</font><font color="green"> $_SERVER['REMOTE_ADDR']</font><p>
<?php echo "Server Remote ADDR value of this URL/Server is:  ".'<font color="red">'.$_SERVER['REMOTE_ADDR'].'</font><br />'; ?>
Note: This is your IP address.
<p><font color="blue">Server variable 10:</font><font color="green"> $_SERVER['SCRIPT_FILENAME']</font><p>
<?php echo "Server Script Filename of this URL/Server is:  ".'<font color="red">'.$_SERVER['SCRIPT_FILENAME'].'</font><br />'; ?>
Note: This is the actual file location of this accessed URL with respect to the server.
<p><font color="blue">Server variable 11:</font><font color="green"> $_SERVER['SERVER_ADDR']</font><p>
<?php echo "Server ADDR value is:  ".'<font color="red">'.$_SERVER['SERVER_ADDR'].'</font><br />'; ?>
Note: This is the Server IP location.
<p><font color="blue">Server variable 12:</font><font color="green"> $_SERVER['GATEWAY_INTERFACE']</font><p>
<?php echo "Server Common gateway interface value of this URL/Server is:  ".'<font color="red">'.$_SERVER['GATEWAY_INTERFACE'].'</font><br />'; ?>
Note: This is the common gateway interface.
<p><font color="blue">Server variable 13:</font><font color="green"> $_SERVER['REQUEST_METHOD']</font><p>
<?php echo "Server Request method value of this URL/Server is:  ".'<font color="red">'.$_SERVER['REQUEST_METHOD'].'</font><br />'; ?>
Note: This is the server request method.
<p><font color="blue">Server variable 14:</font><font color="green"> $_SERVER['SERVER_SOFTWARE']</font><p>
<?php echo "Server software value of this URL/Server is:  ".'<font color="red">'.$_SERVER['SERVER_SOFTWARE'].'</font><br />'; ?>
Note: This is the software used by the server.
<p><font color="blue">Server variable 15:</font><font color="green"> $_SERVER['REMOTE_PORT']</font><p>
<?php echo "Server Remote Port value is:  ".'<font color="red">'.$_SERVER['REMOTE_PORT'].'</font><br />'; ?>
Note: This is your port number used in accessing this content.
<p><font color="blue">Server variable 16:</font><font color="green"> $_SERVER['SERVER_PORT']</font><p>
<?php echo "Server Port Number is:  ".'<font color="red">'.$_SERVER['SERVER_PORT'].'</font><br />'; ?>
Note: This is the server port number used.
<p><font color="blue">Server variable 17:</font><font color="green"> $_SERVER['REMOTE_HOST']</font><p>
<?php echo "The remote host is:  ".'<font color="red">'.$_SERVER['REMOTE_HOST'].'</font><br />'; ?>
Note: This is the remote host.
</body>
</html>


Related posts: