我学会了如何通过在stackoverflow上的另一个答案中的引用来访问,但无法再次找到它。无论如何,以下方法是不安全的还是完全不可靠的?通过引用访问GET和POST
protected function checkVar($requestType, $varname, $checkIfNumber = false)
{
switch($requestType)
{
case 'GET':
$sg = &$_GET;
break;
case 'POST':
$sg = &$_POST;
break;
default:
throw new Exception('Variable `$requestType` is not `GET` or `POST` in AController::checkVar().');
}
if(!isset($sg[$varname])) {
throw new Exception("$requestType variable [$varname] is not set in AController::checkVar().");
} else if(empty($sg[$varname])) {
throw new Exception("$requestType variable [$varname] is empty in AController::checkVar().");
} else if($checkIfNumber) {
if(!ctype_digit($sg[$varname])) {
throw new Exception("$requestType variable [$varname] is not a number in AController::checkVar().");
}
}
return $sg[$varname];
}
我不明白这个功能的重点,但我没有看到很多的问题,除了在同一个功能混合POST和GET。你会想要小心的是,用户不能只在你不知道的情况下抛出一些= extra&data =到&your =函数。 :-) – Chris 2010-08-04 01:43:29
@Chris - 感谢您的回复。我创建此函数的原因是在检查通过GET和POST发送的变量时限制重复执行的代码量。您对额外数据的警告很好,我会再次考虑这个功能。 – 2010-08-04 01:46:18