Meet the super globals.
Time to bring on the next generation of globals - super globals. What are these strange new creatures? Essentially, they are new global arrays which are automatically created. Check out the list below (taken from the manual).
Figure 2.1 - Super Globals (from the manual)
$GLOBALS
Contains a reference to every variable which is currently available within the global scope of the script. The keys of this array are the names of the global variables. $GLOBALS has existed since PHP 3.
$_SERVER
Variables set by the web server or otherwise directly related to the execution environment of the current script. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).
$_GET
Variables provided to the script via HTTP GET. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).
$_POST
Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).
$_COOKIE
Variables provided to the script via HTTP cookies. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).
$_FILES
Variables provided to the script via HTTP post file uploads. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated). See POST method uploads for more information.
$_ENV
Variables provided to the script via the environment. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).
$_REQUEST
Variables provided to the script via any user input mechanism, and which therefore cannot be trusted. The presence and order of variable inclusion in this array is defined according to the variables_order configuration directive. This array has no direct analogue in versions of PHP prior to 4.1.0. See also import_request_variables().
Note: When running on the command line , this will not include the argv and argc entries; these are present in the $_SERVER array.
$_SESSION
Variables which are currently registered to a script's session. Analogous to the old $HTTP_SESSION_VARS array (which is still available, but deprecated). See the Session handling functions section for more information.
As you can see, there are quite a few to choose from. In normal practice, however, you will probably only use a few, with the most likely three being $_POST, $_GET, and $_COOKIE. These will allow you to manage any form data that you might encounter, in addition to cookies.
Assuming that your form uses method="get" then to access an element named fish you would now use $_GET['fish'] instead of $fish. Easy, eh? Please take note of the single quote marks around the variable name - essentially your are using a named index of an array (look up arrays in the manual for more information). The same general principle can be applied to any other methods of transmission - with the added benefit that you can expressly limit the acceptable incoming data to a specific method.
If you wanted to accept information from get, post, or cookie, then you could also use $_REQUEST, but this is considered bad form, as you are essentially going back to bad habits of register_globals = on.
Note: If you are having trouble with a page, and want to find out exactly what data is being passed, the try using the function print_r([SUPERGLOBAL]);. This will output all the contents of the super global you use to replace "[SUPERGLOBAL]" (e.g. $_POST).
Tuesday, April 17, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment