Searching...
Saturday 15 June 2013

PHP Basics - Part 5 :: Variable Part 4 ::: Super Globals 1


PHP’s Superglobal Variables

PHP offers a number of useful predefined variables that are accessible from anywhere within the executing script and provide you with a substantial amount of environment-specific information. You can shift through these variables to retrieve details about the current user session, the user’s operating environment, the local operating environment, and more. PHP creates some of the variables, while the availability and value of many of the other variables are specific to the operating system and web server.
Therefore, rather than attempt to assemble a comprehensive list of all possible predefined variables and their possible values, the following code will output all predefined variables pertinent to any given web server and the script’s execution environment:

foreach ($_SERVER as $var => $value) {
echo "$var => $value <br />";
}

This returns a list of variables similar to the following. Take a moment to peruse the listing produced by this code as executed on a Windows server. You’ll see some of these variables again in the examples that follow:

$_SERVER


As you can see, quite a bit of information is available—some useful, some not so useful. You can display just one of these variables simply by treating it as a regular variable. For example, use this to display the user’s IP address:

printf("Your IP address is: %s", $_SERVER['REMOTE_ADDR']);

This returns a numerical IP address, such as 192.0.34.166.

You can also gain information regarding the user’s browser and operating system. Consider the following one-liner:

printf("Your browser is: %s", $_SERVER['HTTP_USER_AGENT']);

This returns information similar to the following:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729) FirePHP/0.3

Note:- To use the predefined variable arrays, the configuration parameter track_vars must be enabled in the php.ini file. As of PHP 4.03, track_vars is always enabled.

Learning More About the Server and Client

The $_SERVER superglobal contains information created by the web server—details regarding the server and client configuration and the current request environment. Although the value and number of variables found in $_SERVER varies by server, you can typically expect to find those defined in the CGI 1.1 specification (www.w3.org/CGI) . You’ll likely find all of these variables to be quite useful in your applications, some of which include the following:

$_SERVER['HTTP_REFERER']: The URL of the page that referred the user to the current location.
$_SERVER['REMOTE_ADDR']: The client’s IP address.
$_SERVER['REQUEST_URI']: The path component of the URL. For example, if the URL is http://www.example.com/blog/apache/index.html, the URI is /blog/apache/index.html.
$_SERVER['HTTP_USER_AGENT']: The client’s user agent, which typically offers information about both the operating system and the browser.

Retrieving Variables Passed Using GET

The $_GET superglobal contains information pertinent to any parameters passed using the GET method. If the URL http://www.example.com/index.html?cat=apache&id=157 is requested, you could access the following variables by using the $_GET superglobal:

$_GET['cat'] = "apache"
$_GET['id'] = "157"

The $_GET superglobal by default is the only way that you can access variables passed via the GET method. You cannot reference GET variables like this: $cat, $id.

Retrieving Variables Passed Using POST

The $_POST superglobal contains information pertinent to any parameters passed using the POST method. Consider the following form, used to solicit subscriber information:

<form action="subscribe.php" method="post">
<p>
Email address:<br />
<input type="text" name="email" size="20" maxlength="50" value="" />
</p>
<p>
Password:<br />
<input type="password" name="pswd" size="20" maxlength="15" value="" />
</p>
<p>
<input type="submit" name="subscribe" value="subscribe!" />
</p>
</form>

The following POST variables will be made available via the target subscribe.php script:

$_POST['email'] = "jason@example.com";
$_POST['pswd'] = "rainyday";
$_POST['subscribe'] = "subscribe!";

Like $_GET, the $_POST superglobal is by default the only way to access POST variables. You cannot reference POST variables like this: $email, $pswd, and $subscribe.

0 comments:

Post a Comment

 
Back to top!