我正在尝试创建一个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();
}
Run Code Online (Sandbox Code Playgroud)
使用这种技术时,我应该注意哪些缺点或安全监视?
Mar*_*mro 14
检查用户是否允许调用方法:
// 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();
}
Run Code Online (Sandbox Code Playgroud)
否则你将无法控制用户将能够做什么.
<?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 );
}
Run Code Online (Sandbox Code Playgroud)
结合其他一些答案以获得最佳效果.需要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 );
}
Run Code Online (Sandbox Code Playgroud)
这种方法遵循OOP原则,非常冗长(易于维护),并且不要求您维护可以调用哪些类的数组,哪些不能.
| 归档时间: |
|
| 查看次数: |
1133 次 |
| 最近记录: |