我正在尝试创建一个 AJAX 脚本,它将采用两个 GET 变量、类和方法,并将它们映射到我们设计的方法(类似于 CodeIgniter 对 ajax 的作用,我很确定)。由于我依靠用户输入来确定要执行的类和方法,因此我担心黑客可能会通过某种方式利用该技术为他们谋取利益。
代码:
//Grab and clean (just in case, why not) the class and method variables from GET
$class = urlencode(trim($_GET['c']));
$method = urlencode(trim($_GET['m']));
//Ensure the passed function is callable
if(method_exists($class, $method)){
$class::$method();
}
在使用此技术时,我应该注意哪些缺点或安全注意事项?
最佳答案
检查是否允许用户调用方法:
// methods that user can call:
$user_methods = array("method1", "method2", "method3", );
//Ensure the passed function is callable
if(method_exists($class, $method) and in_array($method, $user_methods){
$class::$method();
}
否则您将无法控制用户可以做什么。
关于php - 根据用户输入安全地调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11414372/