Wednesday, 12 January 2011

PHP’s Superglobal Variables

PHP offers a number of useful predefined variables that are accessible from anywhere within the
executing script and provide us with a substantial (quan trọng, giá trị) amount of environment-specific information.

We can sift (sàng lọc )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:


This returns a list of variables similar to the following.




As we can see, many information are available =>some useful, some not so useful. Wecan
display just one of these variables simply by treating it as a regular variable. For example, use this code to display the user’s IP address:


This returns a numerical IP address:

We can also gain information regarding the user’s browser and operating system by following code:


This returns information similar to the following:



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.

We will likely find all of these variables to be quite useful in our 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 (thích hợp, có liên quan) to any parameters passed using the GET method.

If the URL http://www.example.com/index.html?cat=apache&id=157 is requested, we could access the following variables by using the $_GET superglobal:


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 (nài nỉ, lôi kéo) subscriber (thuê bao) information:


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


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

Retrieving Information Stored Within Cookies


The $_COOKIE superglobal stores information passed into the script through HTTP cookies. Such cookies are typically set by a previously executed PHP script through the PHP function setcookie().

For example, suppose that we use setcookie() to store a cookie named pvhung.com with the value ab2213. We could later retrieve that value by calling $_COOKIE["pvhung.com"].

Retrieving Information About Files Uploaded Using POST

The $_FILES superglobal contains information regarding data uploaded to the server via the POST method.

This superglobal is a tad (1 ít, 1 chút) different from the others in that it is a two-dimensional array containing five elements.

The first subscript refers to the name of the form’s file-upload form element;
the second is one of five predefined subscripts that describe a particular attribute of the uploaded file:

$_FILES['upload-name']['name']: The name of the file as uploaded from the client
to the server.

$_FILES['upload-name']['type']: The MIME type of the uploaded file. Whether
this variable is assigned depends on the browser capabilities.

$_FILES['upload-name']['size']: The byte size of the uploaded file.

$_FILES['upload-name']['tmp_name']: Once uploaded, the file will be assigned a
temporary name before it is moved to its final location.

$_FILES['upload-name']['error']: An upload status code. Despite the name, this
variable will be populated even in the case of success. There are five possible
values:
  • UPLOAD_ERR_OK: The file was successfully uploaded.
  • UPLOAD_ERR_INI_SIZE: The file size exceeds the maximum size imposed by the
  • upload_max_filesize directive.
  • UPLOAD_ERR_FORM_SIZE: The file size exceeds the maximum size imposed by an
  • optional MAX_FILE_SIZE hidden form-field parameter.
  • UPLOAD_ERR_PARTIAL: The file was only partially uploaded.
  • UPLOAD_ERR_NO_FILE: A file was not specified in the upload form prompt.
Learning More About the Operating System Environment

The $_ENV superglobal offers information regarding the PHP parser’s (phân tách cú pháp) underlying server environment. Some of the variables found in this array include the following:
  • $_ENV['HOSTNAME']: The server hostname
  • $_ENV['SHELL']: The system shell
Caution: PHP supports two other superglobals, namely $GLOBALS and $_REQUEST.

The $_REQUEST superglobal is a catch-all of sorts, recording variables passed to a script via the GET, POST, and Cookie methods.

The order of these variables doesn’t depend on the order in which they appear in the sending script; rather, it depends on the order specified by the variables_order configuration directive

The $GLOBALS superglobal array can be thought of as the superglobal superset and contains a comprehensive (bao hàm toàn diện) listing of all variables found in the global scope.

Retrieving Information Stored in Sessions


The $_SESSION superglobal contains information regarding all session variables.
Registering session information allows us the convenience of referring to it throughout our entire web site, without the hassle of explicitly passing the data via GET or POST.

Variable Variables

On occasion, we may want to use a variable whose content can be treated dynamically as a variable in itself. Consider this typical variable assignment:

$recipe = "spaghetti";

We can treat the value spaghetti as a variable by placing a second dollar sign in front
of the original variable name and again assigning another value:

$$recipe = "& meatballs";

This in effect assigns & meatballs to a variable named spaghetti.

Therefore, the following two snippets of (mẩu tin, đoạn trích ngắn) code produce the same result:

echo $recipe $spaghetti;
echo $recipe ${$recipe};

The result of both is the string spaghetti & meatballs.

Constants

A constant is a value that cannot be modified throughout the execution of a program. Constants are particularly useful when working with values that definitely will not require modification, such as Pi (3.141592) or the number of feet in a mile (5,280).

Once a constant has been defined, it cannot be changed (or redefined) at any other point of the program. Constants are defined using the define() function.

Defining a Constant
The define() function defines a constant by assigning a value to a name. Its prototype follows:


If the optional parameter case_insensitive is included and assigned TRUE, subsequent references to the constant will be case insensitive. Consider the following example in which the mathematical constant Pi is defined:

define("PI", 3.141592);

The constant is subsequently used in the following listing:
printf("The value of Pi is %f", PI);
$pi2 = 2 * PI;
printf("Pi doubled equals %f", $pi2);
This code produces the following results:



There are several points to note regarding the previous listing.
The first is that constant references are not prefaced (mở đầu) with a dollar sign.
The second is that we can’t redefine or undefine the constant once it has been defined (e.g., 2*PI); if we need to produce a value based on the constant, the value must be stored in another variable. Finally, constants are global; they can be referenced anywhere in our script.

No comments:

Post a Comment