These tutorials are about PHP and its use for server-side Web programming.
If you are not familiar with arrays, then you probably want to review those before looking at this section.
In order to store information coming from both the client and server and pertaining to the current execution of a script. PHP has a series of arrays defined. These arrays are sometimes referred to as the superglobals since, in a standard PHP configuration, they are directly accessible from any point in the code, within functions or without. Depending on your cofiguration, you may need to use the global keyword to include these arrays in a function, however, the superglobal array $GLOBALS[] is always accessible from anywhere in the code.
You will also find these arrays referred to as environmental variables, since they store information about the environment in which the script is running. I tend to use this term throughout my notes, snice it more accurately reflects the purpose of these arrays. Each array gains it name from the portion of the environment it is responsible for tracking.
Most global array values have three different ways of being referenced. The array has a short-form and a long-form for the name, and the array element can be addressed indivdually as its own variable. There are differences on when you can use each format.
For instance, the array $HTTP_POST_VARS[] has a short form of $_POST[].This is the result of different PHP versions and changing standards. $HTTP_POST_VARS[] is the old array name and is not guaranteed to work on future version of PHP. $_POST[] is the new array name and does not work on anything before PHP4. Unless you are working on an older server, it is recommended that you use the newer short version. It also has the benefit of being less to type.
The global arrays are all associative arrays. This means the value is associated with a name that is the index key for the contents and, sensibly, describes what the value represents. For instance, $_SERVER["SCRIPT_NAME"] contains the URL path to the current page.
Sometimes you can also access the values of the array directly by variable name. This varialbe name is the same as the index key for the array. Thus, the variable name for $_SERVER["SCRIPT_NAME"] would be $SCRIPT_NAME.
The sometimes part comes into play because in order to do this, the PHP setting for register_globals needs to be turned on. When turned on,this setting allows PHP to create variables based on all the global array values automatically. In PHP3 this is turned on by default. In PHP4 this is turned off by default. The reason for this shift of policy is security. It is possible to hack the system by sending it bogus variables that will capture system information and return it to the user when the global variables are automatically registered. Manual processing from the array allows the programmer to screen all incoming data first.
At its most basic, the screening process works like this:
$formField1 = '';
foreach ($_POST as $key => $value) {
if (isset(${$key}) {
${$key} = $value;
}
}
What this could would do would be to only accept a value from the $_POST[] array if it had an index key of $formField1 and assign it to the variable $formField1. Any variables the programmer was not expecting would be ignored, preventing the user from trying to pass bogus variables. The isset function returns true if a variable already has a value, an empty string being a legal value, Thus the code would only overwrite existing variables and not allow any new ones to be created. Note the use of variable variables so we don't have to test each internal value separately,
So what are the global arrays? Well here is the list.
| Old Form | New Form | Description |
|---|---|---|
-- |
$GLOBALS[] |
The complete list of all global variables, including user defined variables at the global level. |
$HTTP_GET_VARS[] |
$_GET[] |
All variables received as part of a query string in the requesting URL, or HTML form data transmitted using the GET method. |
$HTTP_POST_VARS[] |
$_POST[] |
All variables recieved as an inline posted data set, normally through using the POST method in an HTML form. |
$HTTP_POST_FILES[] |
$_FILES[] |
References to all files received, most commonly from HTML forms, using the POST method. |
$HTTP_COOKIE_VARS[] |
$_COOKIE[] |
Any cookies returned from the client. The index key name matches the cookie name. |
-- |
$_REQUEST[] |
A more recent addition that stores all user variables, including elements from the $_GET[], $_POST[], and $_COOKIE[] arrays. Prior to PHP4.3, this also includes the $_FILES[] array. |
$HTTP_SERVER_VARS[] |
$_SERVER[] |
Information about the server session and the HTTP connection with the client. |
$HTTP_ENV_VARS[] |
$_ENV[] |
Information about the server environment and system defined values. |
$HTTP_SESSION_VARS[] |
$_SESSION[] |
IF PHP is being used for session management, this array is to store any session variables that need to be stored on the server between calls from the client. |
$_SERVER[] Array
Most of the global arrays store various types of values defined by the programmer. This means there isn't much that can be said about their contents since that is entirely specific to the current script. The exceptions are the $_SERVER[] array, which stores system information related to the execution of the current script, and $_ENV[] which stores information about the general system environment. These have many predefined values which we can talk about and which are useful to be aware of. For now we will focus on the $_SERVER[] array. It has values that are of immediate use to us in learning PHP. The elements in the $_ENV[] array only come in to place for advanced PHP coding and server management. Although, if you want to know the default mail message for the system, it can found under $_ENV["MAILMSG"]
The $_SERVER[] array maintains server information that pertains to hte current session and information received from the client in the HTTP header of the client request. The names of these elements, wwhere relevant, adhere to the current standards for CGI scripting. Others come from the HTTP specifications,usually matching the names of the header fields. The following list is a list of most of the elements in the $_SERVER[] array and they are used for.
Here are the most common HTTP elements. Note that there are many other HTTP headers, including many of no use to PHP, so it ignores them, but these are the ones that contain information useful to successfully serving up content to a client.
HTTP_ACCEPTtext/html, image/png, image/jpeg, image/gif, */*HTTP_ACCEPT_CHARSETutf-8, utf-16, iso-8859-1, *HTTP_ACCEPT_ENCODINGdeflate, gzip, x-gzip, identity, HTTP_ACCEPT_LANGUAGEen,ja,frHTTP_CONNECTIONKeep-Alive.HTTP_HOSTHTTP_REFERERHTTP_USER_AGENTOpera/7.23 (Windows NT 5.1; U) [en]Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)OPWW-SDK/62 UP.Browser/6.2.2.1/208 (GUI) MMP/2.0Many of the elements that show up are from the CGI specifications. Here is the list of those and what they contain.
SERVER_SOFTWAREApache/1.3.29 (Unix) PHP/4.3.4SERVER_NAMESERVER_ADDRGATEWAY_INTERFACECGI/1.1.SERVER_PROTOCOLHTTP/1.1.SERVER_PORT80.REQUEST_METHODGET or POST.PATH_INFOPATH_TRANSLATED/~kantopet/ as specified in the URL maps to /home/kantopet/homepage/ on the server.SCRIPT_NAMEREQUEST_URI and PHP_SELF in the array. The first is a the server version of the data field, the second the PHP version. The second is the more commonly used array element in PHP programming.SCRIPT_FILENAMESCRIPT_NAME. This contains the server path to the resource. This is not really part of the CGI standard, but it is a good place to include it.QUERY_STRINGREMOTE_HOSTREMOTE_ADDRAUTH_TYPEREMOTE_USERREMOTE_IDENTidentd identification check (RFC 931). This string is easily spoofed and should never be used for authentication.CONTENT_TYPECONTENT_LENGTH
The $_SERVER[] array also includes some general info provided by the server for its own reference. Here is the basic set.
DOCUMENT_ROOTPATHREMOTE_PORTSERVER_ADMINSERVER_SIGNATUREApache/1.3.29 Server at academ.hvcc.edu Port 80
These pages can be found at:
[http://academ.hvcc.edu/~kantopet/]
Copyright 2003 -- Peter L. Kantor[kantopet@hvcc.edu]
Last Updated January 2003