php – 根据用户输入安全地调用函数

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 根据用户输入安全地调用函数脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一个 AJAX脚本,它将采用两个GET变量,类和方法,并将它们映射到我们设计的方法(类似于CodeignITer如何为ajax行事,我很确定).由于我依赖于用户输入来确定要执行的类和方法,所以我担心黑客可能会有某种方式将这种技用于他们的优势.

代码

//Grab and clean (just in case,why not) the class and method VARiables @R_304_2150@ 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 ) &amp;& $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原则,非常冗长(易于维护),并且不要求您维护可以调用哪些类的数组,哪些不能.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 根据用户输入安全地调用函数全部内容,希望文章能够帮你解决php – 根据用户输入安全地调用函数所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。