php – 根据用户输入安全地调用函数
发布时间:2020-12-13 22:36:00 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试创建一个 AJAX脚本,它将采用两个GET变量,类和方法,并将它们映射到我们设计的方法(类似于CodeIgniter如何为ajax行事,我很确定).由于我依赖于用户输入来确定要执行的类和方法,所以我担心黑客可能会有某种方式将这种技术用于他们的优势. 代码: //Gra
我正在尝试创建一个
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(); } 使用这种技术时,我应该注意哪些缺点或安全监视? <?php class AjaxCallableFunction { public static $callable_from_ajax = TRUE; } $class = $_POST['class']; $method = $_POST['method']; if ( class_exists( $class ) && isset( $class::$callable_from_ajax ) && $class::$callable_from_ajax ) { call_user_func( $class,$method ); } 结合其他一些答案以获得最佳效果.需要PHP 5.3.0或更高版本.你甚至可以实现一个接口 <?php interface AjaxCallable {} class MyClass implements AjaxCallable { // Your code here } $class = $_POST['class']; $method = $_POST['method']; if ( class_exists( $class ) && in_array( 'AjaxCallable',class_implements( $class ) ) ) { call_user_func( $class,$method ); } 这种方法遵循OOP原则,非常冗长(易于维护),并且不要求您维护可以调用哪些类的数组,哪些不能. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |