【问题标题】:Is it possible to call a particular function in PHP using GET data? [duplicate]是否可以使用 GET 数据在 PHP 中调用特定函数? [复制]
【发布时间】:2014-09-22 13:39:18
【问题描述】:

我有一个文件 functions.php 有多个功能,如:

function do_this(){
}

function do_that(){
}

现在,我正在考虑做一些奇怪的事情,比如从 URL 调用该函数:

functions.php?func=do_that

现在,我将这行代码添加到 functions.php

if(isset($_GET["func"])){
    $func = $_GET["func"];
    //How to call that function with name "$func"

}

那里有伟大的大脑吗?让我知道。如果那是不可能的,那么也让我知道。因为我必须在我的工作中考虑一种新的方法。

【问题讨论】:

  • 我会告诉你这是可能的,因为我这样做了。 :) 享受吧!
  • 这在 PHP 中称为variable function。极其小心!仅执行可接受值的数组(白名单)中的函数。否则,您将引入代码注入漏洞(严重)
  • 是的。您可以使用 call_user_func(); php.net/manual/en/function.call-user-func.php
  • 建议你做类似$allowed = array('do_this', 'do_that'); if (!in_array($_GET['func'], $allowed)) { // ERROR, don't execute! }

标签: php


【解决方案1】:

您需要做的就是在获得 $_GET 变量时调用该函数,如下所示:

if(isset($_GET["func"])){
    $func = $_GET["func"];
    $func();

}

【讨论】:

  • 所需要的还不止这些。需要更多的保护来限制对可接受函数的调用。例如script.php?func=phpinfo 会将服务器信息转储到屏幕上。
猜你喜欢
  • 2013-01-11
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多