例如,一个简单的 MVC 类型系统:
/api/class/method
使用 重写为 PHP 变量.htaccess/nginx.conf
,然后执行以下操作:
<?php
// Set up class + method variables
$className = some_class_filter($_GET['class']);
$method = some_method_filter($_GET['method']);
// Check if class exists and execute
if(file_exists(BASE . "/controllers/" . $className . ".class.php")) {
require BASE . "/controllers/" . $className . ".class.php";
$$className = new $className();
// Execute the method
$$className->$method();
} else {
// Spit out some error based on the problem
}
?>
这是非常糟糕的做法吗?如果这是不好的做法,有人可以解释一下为什么吗?如果是这样,有没有更好的方法来做我正在做的事情?
编辑本质上,我使用变量变量的原因是为了简化核心系统的扩展——即——添加一个新的控制器既好又简单。我绝对理解允许在没有某种过滤器的情况下实例化基本上任何函数或类的安全风险。
'some_filter_here' 可能是允许的控制器列表 - 此处有些人提到的白名单。