This section provides a tutorial example on how to promote (or register) keys and values in the $_REQUEST array as global variables so that you don't have to use the array notation to access their values.
To access information stored in the $_REQUEST array, you can use the normal array element notation,
$_REQUEST[$key]. For example, $_REQUEST["lang"] would return "PHP" in the example script used in the previous section.
But key-value pairs in $_REQUEST can also be promoted (or registered) as standalone global variables using one
of two ways described below:
1. To register $_REQUEST key-value pairs as global variables on the entire server, edit \php\php.ini and set:
register_globals = on
With this setting, the PHP engine will automatically create a global variable for each key in $_REQUEST with
the key name as the variable name. of course, the value associated with the key will be copied to the variable at the same time.
For example, if $_REQUEST["lang"] contains "PHP", $lang will created with the value "PHP".
2. To register $_REQUEST key-value pairs as local variables for the current script execution only,
use the following function:
import_request_variables("GPC",$prefix);
where "GPC" indicates that key-value pairs copied from $_GET, $_POST and $_COOKIE will be registed as local variables.
$prefix defines a prefix string that are to be added to variable names.
To test this, I turned on the register_globals setting in php.ini and wrote this sample script, RequestVariables.php:
<?php # RequestVariables.php
# Copyright (c) 2002 by Dr. Herong Yang
#
print "<pre>\n";
print "\nContents of \$_REQUEST:\n";
foreach ($_REQUEST as $k => $v) {
print " $k = $v\n";
}
#
print "\nLocal imported variables from the request:\n";
import_request_variables("GPC","r_");
print " \$r_lang = $r_lang\n";
print " \$r_search = $r_search\n";
#
print "\nGlobaly imported variables from the request:\n";
print " \$lang = $lang\n";
print " \$search = $search\n";
print "</pre>\n";
?>
Open this script with http://localhost/RequestVariables.php?lang=PHP&search, you will get:
Contents of $_REQUEST:
lang = PHP
search =
Local imported variables from the request:
$r_lang = PHP
$r_search =
Globaly imported variables from the request:
$lang = PHP
$search =