PHP Eval替代生成动态if条件

I have the following php code where I'm trying to generate a dynamic if condition using eval:

$categoryId = $_REQUEST['category_id'];
$locationId = $_REQUEST['location_id'];

$recordId = 1;

$criteria = [(!empty($categoryId) ? '(isInCategory($recordId, $categoryId))' : ''),
             (!empty($locationId) ? '(isInLocation($recordId, $locationId))' : '')];

$check = implode(' && ', array_filter($criteria));

// $check will be  
// (isInCategory($recordId, $categoryId)) && (isInLocation($recordId, $locationId))
// if $categoryId and $locationId are not empty

if(eval("return $check;")) 
{ 
    echo 'true'; 
}
else
{
   echo 'false';
}

The problem is the entire code itself is within an eval statement so basically I'm ending up with an eval inside an eval which is throwing an error - can't really troubleshoot the error because all I get is a friendly error page so I'm assuming that is the case i.e. you cant use eval inside another eval???

I'm forced to use a propriety product so this is my limitation. Is there a way I can refactor the eval in the above if statement via some other technique.

I read some stuff about using call_user_func() but I'm new to this. Any help would be appreciated.

dongyi7966
dongyi7966 只是谷歌吧。您使用eval做的事情基本上是让网络上的任何人在您的服务器上执行他们想要的任何代码。这绝不是您想要的,因为整个服务器都受到了损害。
一年多之前 回复
dongyi2006
dongyi2006 我不知道你的远程代码执行是什么意思。请您使用一些代码示例来解释和演示。
一年多之前 回复
dougu1952
dougu1952 您刚刚创建了远程代码执行。只要忘记语句eval()甚至存在,然后强迫自己提出问题,我怎么能在没有eval的情况下做到这一点?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐