Searching...
Saturday 15 June 2013

PHP Basics - Part 5 :: Variables Part 5 ::: Super Globals 2


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()(more about functions in php). For example, suppose that you use setcookie() to store a cookie named example.com with the value ab2213.  You could later retrieve that value by calling $_COOKIE["example.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 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 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 listing of all variables found in the global scope. Although it may be tempting, you shouldn’t use these superglobals as a convenient way to handle variables because it is insecure.

Retrieving Information Stored in Sessions

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

Variable Variables

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

$actor = "Jai";

Interestingly, you can treat the value Jai as a variable by placing a second dollar sign in front of the original variable name and again assigning another value:

$$actor = "& Veeru";

This in effect assigns & Veeru to a variable named Jai. Therefore, the following two snippets of code produce the same result:

echo $actor $Jai;
echo $actor ${$actor};

The result of both is the string Jai & Veeru.


0 comments:

Post a Comment

 
Back to top!