register_globals = Off
Since PHP 3, the following global arrays existed for form data:
-
$HTTP_GET_VARS
All data provided usingGET
-
$HTTP_POST_VARS
All data provided usingPOST
-
$HTTP_REQUEST_VARS
All data provided usingGET
orPOST
, or via cookies (use not recommended)
These arrays are global; therefore, you have to use the global
keyword to up level them to global scope if you use them within a function:
function processData() { global $HTTP_POST_VARS; // now you may access $HTTP_POST_VARS }
However, these arrays can be deactivated (PHP 5 onward), as well, using this php.ini
directive:
register_long_arrays = Off
Therefore, the following is the only recommended method to access form data today in PHP:
-
$_GET
forGET
data -
$_POST
forPOST
data -
$_REQUEST
forPOST
,GET
, and cookies (not recommended)
The keys of these arrays are the names of the form values. The $_*
arrays are so-called superglobal arraysthat is, you do not have to use the global
keyword to get them into global scope; they are already available within functions.
When you have decided which superglobal array to use (depending on the form's method
), accessing form data is easy: $_GET[<formfieldname>]
or $_POST[<formfieldname>]
retrieves the value in the form element. Table 4.1 shows which data is returned for which form field type.
Form Field Types and Data Returned in $_GET
/$_POST
Form Field Type |
Data Returned |
---|---|
Text field |
Text in field |
Password field |
Text in field (clear text, not encrypted) |
Multiline text field |
Text in field |
Hidden field |
|
Radiobutton |
|
Checkbox |
|
Selection list |
|
Multiple selection list |
|
Submit button |
|
Two remaining form field types, graphical Submit buttons and file uploads, are covered specifically later in this chapter.