0 php
我是PHP的初学者,我接触过处理过的HTML表单,我正在学习如何使用它来做更多...
我正在使用这个功能(并且基于Google,因此有很多人)但我真的想了解它在做什么......
function pt_register()
{
$num_args = func_num_args();
$vars = array();
if ($num_args >= 2) {
$method = strtoupper(func_get_arg(0));
if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) {
die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV');
}
$varname = "HTTP_{$method}_VARS";
global ${$varname};
for ($i = 1; $i < $num_args; $i++) {
$parameter = func_get_arg($i);
if (isset(${$varname}[$parameter])) {
global $$parameter;
$$parameter = ${$varname}[$parameter];
}
}
} else {
die('You must specify at least two arguments');
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以用英语为我解决这个问题吗?
看起来它正试图成为register_globals的替代品
function pt_register()
{
// Look at the arguments passed in...
$num_args = func_num_args();
$vars = array();
// .. we need at least 2 arguments
if ($num_args >= 2) {
// $method is the middle part of name of one of PHPs old-style variables
$method = strtoupper(func_get_arg(0));
if (($method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) {
die('The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV');
}
// $varname is the whole name of the variable
$varname = "HTTP_{$method}_VARS";
// Make the global variable (for example HTTP_SESSION_VARS) accessible from this function
// ${$varname} is using a technique called "variable variables"
// If $varname == "HTTP_SESSION_VARS" then $$varname (or ${$varname}) is the same as $HTTP_SESSION_VARS
global ${$varname};
// For each argument after the method
for ($i = 1; $i < $num_args; $i++) {
$parameter = func_get_arg($i);
// If the parameter exists in the global variable...
if (isset(${$varname}[$parameter])) {
// .. make it global...
global $$parameter;
// ... and set its value
$$parameter = ${$varname}[$parameter];
}
}
} else {
die('You must specify at least two arguments');
}
}
Run Code Online (Sandbox Code Playgroud)
所以,例如:pt_register('SESSION', 'foo');确实如此
function example()
{
global $HTTP_SESSION_VARS;
global $foo;
$foo = $HTTP_SESSION_VARS['foo'];
}
Run Code Online (Sandbox Code Playgroud)
恕我直言,这个剧本已经过时而且邪恶!superglobals $ _SESSION等意味着你不应该这样做