$ _REQUEST超级数组未在$ GLOBALS数组中初始化 [英] $_REQUEST superarray not initialized in $GLOBALS array
问题描述
问题
因此,我具有此功能,可以从$ _REQUEST,$ _ POST,$ _ GET或$ _COOKIE数组中检索并继续处理数据.我知道仅从函数调用中使用哪个数组.简化前:
function gg( $name, $type="_REQUEST" ) {
return isset( $GLOBALS[$type][$name] ) ? $GLOBALS[$type][$name] : false;
}
它非常适合以下呼叫:
gg('var', '_GET');
gg('var2', '_POST');
但由于以下原因而严重失败:
gg('var');
// or
gg('var', '_REQUEST');
我设法将这个问题简化为2行:
print_r( $GLOBALS['_REQUEST'] ); // this line returns nothing...
print_r( $_REQUEST ); // ...UNLESS this line is present anywhere in the code
现在,我的明显问题是:是否有必要初始化$ GLOBALS中存在的$ _REQUEST数组?
其他信息:
php: 5.3.3-7
apache: 2.2.16
also I'm running on CGI/FastCGI
编辑&解决方案
1
此处最简单的解决方案是编辑php.ini并在其中更改 auto_globals_jit 的值从开到关.
auto_globals_jit Off
2
相反,您可以在内部使用 ini_set()您的源文件,但是它对我不起作用...
ini_set("auto_globals_jit", "Off");
3
另一种解决方案是对所有内容使用 $ GLOBALS 数组除了 $ _REQUEST 之外,对于$ _REQUEST请求,直接调用$ _REQUEST数组:D
if($type == "REQUEST") return $_REQUEST[$name];
else return ${"_".$type}[$name]; // or $GLOBALS["_".$type][$name] if previous won't work
无法在我的设置中复制它,因此可能是CGI问题?作为解决方法,您可以执行以下操作……
function gg( $name, $type="_REQUEST" ) {
return isset( ${$type}[$name] ) ? ${$type}[$name] : false;
}
可能会引起关注:
自PHP 5.4起,$ GLOBALS现在已及时初始化.这表示 现在,不使用$ GLOBALS变量是一个优势 避免初始化它的开销. http://www.php.net/manual/en/reserved. variables.globals.php
更新.请参见发布:
PROBLEM
So, I have this function to retrieve and proceed data from $_REQUEST, $_POST, $_GET or $_COOKIE arrays. I know which array to use only from function call. Simplified ex:
function gg( $name, $type="_REQUEST" ) {
return isset( $GLOBALS[$type][$name] ) ? $GLOBALS[$type][$name] : false;
}
And it works perfectly for calls like:
gg('var', '_GET');
gg('var2', '_POST');
But fails dramatically for:
gg('var');
// or
gg('var', '_REQUEST');
I managed to simplify this problem thou to 2 lines:
print_r( $GLOBALS['_REQUEST'] ); // this line returns nothing...
print_r( $_REQUEST ); // ...UNLESS this line is present anywhere in the code
Now, my obvious question is: Is there any necessity to initialize this $_REQUEST array to be present in $GLOBALS?
additional info:
php: 5.3.3-7
apache: 2.2.16
also I'm running on CGI/FastCGI
EDIT & SOLUTION
1
As found here the easiest solution would be to edit php.ini and change there value of auto_globals_jit from On to Off.
auto_globals_jit Off
2
Instead of this you can use ini_set() inside of your source file, however it didn't work for me...
ini_set("auto_globals_jit", "Off");
3
Yet another solution is to use $GLOBALS array to everything except $_REQUEST and for $_REQUEST requests call directly to $_REQUEST array :D
if($type == "REQUEST") return $_REQUEST[$name];
else return ${"_".$type}[$name]; // or $GLOBALS["_".$type][$name] if previous won't work
Couldn't replicate this on my setup so it could possibly be CGI issue? As a workaround you could do something like this...
function gg( $name, $type="_REQUEST" ) {
return isset( ${$type}[$name] ) ? ${$type}[$name] : false;
}
Might be of interest:
As of PHP 5.4 $GLOBALS is now initialized just-in-time. This means there now is an advantage to not use the $GLOBALS variable as you can avoid the overhead of initializing it. http://www.php.net/manual/en/reserved.variables.globals.php
Update. See Post:
$_REQUEST not created when using variable variables?
这篇关于$ _REQUEST超级数组未在$ GLOBALS数组中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!