PHP provides access to a number of predefined functions and variables.
For an exhaustive overview of the predefined variables available in the PHP programming language please check out the related section on php.net website, from which the following complete list was derived.
Superglobals — Superglobals are built-in variables that are always available in all scopes
$GLOBALS — References all variables available in global scope
$_SERVER — Server and execution environment information
$_GET — HTTP GET variables
$_POST — HTTP POST variables
$_FILES — HTTP File Upload variables
$_REQUEST — HTTP Request variables
$_SESSION — Session variables
$_ENV — Environment variables
$_COOKIE — HTTP Cookies
$php_errormsg — The previous error message
$HTTP_RAW_POST_DATA — Raw POST data
$http_response_header — HTTP response headers
$argc — The number of arguments passed to script
$argv — Array of arguments passed to script
In this section we focus on a couple of those built-in variables that we may use in our scripts. In particular we will see how the $_SERVER array provides access to information on our working environment and we will just hint, for now, at the $_POST array, that let us retrieve data submitted by website visitors through web forms, an essential bit of the web applications that we will build later on.
The $_SERVER variable
The $_SERVER variable, an array with several predefined keys, provides access to many so called “environmental” variables about our working environment and our website visitors.
To check all available keys for the $_SERVER array, we can use the array_keys() function that we have introduced in the previous section about PHP arrays basics.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $server_array_keys = array_keys($_SERVER); // Let us format the output nicely in the form of an unordered list embedded in a paragraph echo "<p><ul>\n"; foreach($server_array_keys as $key){ echo "<li>$key</li>\n"; // We include \n so that in the source code each list element will be on it's own line } echo "</ul></p>\n"; ?> |
At the time of this writing, the code above will output the following result on our test Linux server:
- HTTP_HOST
- HTTP_CONNECTION
- HTTP_UPGRADE_INSECURE_REQUESTS
- HTTP_USER_AGENT
- HTTP_ACCEPT
- HTTP_ACCEPT_ENCODING
- HTTP_ACCEPT_LANGUAGE
- PATH
- SERVER_SIGNATURE
- SERVER_SOFTWARE
- SERVER_NAME
- SERVER_ADDR
- SERVER_PORT
- REMOTE_ADDR
- DOCUMENT_ROOT
- REQUEST_SCHEME
- CONTEXT_PREFIX
- CONTEXT_DOCUMENT_ROOT
- SERVER_ADMIN
- SCRIPT_FILENAME
- REMOTE_PORT
- GATEWAY_INTERFACE
- SERVER_PROTOCOL
- REQUEST_METHOD
- QUERY_STRING
- REQUEST_URI
- SCRIPT_NAME
- PHP_SELF
- REQUEST_TIME_FLOAT
- REQUEST_TIME
You can find the meaning of each one of these keys here. Let’s focus on a few of those.
The general syntax for retrieving the value for each one of these keys is the usual syntax for array with keys: the array name followed by the name of the key enclosed in square brackets:
1 2 3 |
$_SERVER['key_name'] |
SERVER_ADDR
the SERVER_ADDR key returns the IP address of the server on which the script is running, the web server:
1 2 3 4 5 6 7 8 9 |
<?php $server_ip_address = $_SERVER['SERVER_ADDR']; echo "<p>The IP address of the server running this script is <strong>$server_ip_address</strong></p>\n"; ?> |
REMOTE_ADDR
the REMOTE_ADDR key returns the IP address of the visitor’s computer, where the web browser is running:
1 2 3 4 5 6 7 8 9 |
<?php $visitor_ip_address = $_SERVER['REMOTE_ADDR']; echo "<p>Welcome visitor from <strong style=\"color:red\">$visitor_ip_address</strong></p>\n"; ?> |
DOCUMENT_ROOT
the DOCUMENT_ROOT key returns the path of the web server’s web root, as defined in the Apache configuration file.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $visitor_ip_address = $_SERVER['REMOTE_ADDR']; $web_root_path = $_SERVER['DOCUMENT_ROOT']; echo "<p>Welcome visitor from <strong style=\"color:red\">$visitor_ip_address</strong></p>\n"; echo "<p>The web root of our web server is located at <strong style=\"color:blue\">$web_root_path</strong> within the filesystem</p>\n"; // By default, on recent Apache2 installations, the web root's path is: /var/www/html ?> |
SCRIPT_NAME
The SCRIPT_NAME key returns the path of the script currently running.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $visitor_ip_address = $_SERVER['REMOTE_ADDR']; echo "<p>Welcome visitor from <strong style=\"color:red\">$visitor_ip_address</strong></p>\n"; echo "<p>You are currently running the following script: ". ."</p>\n"; ?> |
As an exercise, try running the following script on you own web server and check out the results!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $visitor_ip_address = $_SERVER['REMOTE_ADDR']; $web_root_path = $_SERVER['DOCUMENT_ROOT']; $server_ip_address = $_SERVER['SERVER_ADDR']; $current_script_name = $_SERVER['SCRIPT_NAME']; echo "<p>Welcome visitor from <strong style=\"color:red\">$visitor_ip_address</strong></p>\n"; echo "<p>You are currently running the <strong style=\"color:red\">$current_script_name</strong> script</p>\n"; echo "<p>We are delighted you came to visit our web server at <strong style=\"color:red\">$server_ip_address</strong> whose web root is located at <strong style=\"color:red\">$web_root_path<strong></p>\n"; ?> |
The $_POST variable
The $_POST variable is, as $_SERVER, an array with keys. However, unlike $_SERVER, the keys are not predefined but rather created “on the fly” when the user presses the “Submit” button of a web form. After the user submits the data from a web form, the $_POST array gets populated with one key for each of the different fields that were present in the web form. We will go through these mechanisms in great detail on the next chapter. For now, let us just say that each field of a web form is associated with a “name” attribute. For example if a user submits a form that contains fields with name=”user” and name=”sequence”, the user and sequence keys will be generated in the $_POST array, and will point to the values given to these two fields within the web form. We can then, within the PHP script, collect these values:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // Rather than visiting this script directly, the user is directed here after pressing the submit button of a web form // If you run this code example as such it will not work $user = $_POST['user']; $sequence = $_POST['sequence']; echo "<p>Welcome $user</p>\n"; echo "<p>This is the sequence you sent us $sequence</p>\n"; ?> |
So in short, $_POST is used by a PHP script to collect data from a submitted web form. This will become more clear on reading the next chapter.
Chapter Sections
[pagelist include=”435″]
[siblings]
Great reference. I was looking for some additional information not listed on php.net
You’re missing a few. Not sure if you wanted a complete list. HTTP_X_FORWARDED_FOR
Thanks