简体   繁体   English

我是否需要清理从文件中获取的 php 代码?

[英]Do I need to sanitize php code im fetching from files?

I am using ajax and php for my current application right now.我现在正在为我当前的应用程序使用 ajax 和 php。 I give an action for each ajax request I make.我为我提出的每个 ajax 请求提供一个操作。 I know that I can just give different urls to each individual request, but I want all my actions to be in one file.我知道我可以为每个单独的请求提供不同的 url,但我希望我的所有操作都在一个文件中。 However, I want the one file to catch every file in a specific directory get the action from there and put it in the main file.但是,我希望一个文件可以捕获特定目录中的每个文件,从那里获取操作并将其放入主文件中。 So I would loop each individual file and fetch the content in them.所以我会循环每个单独的文件并获取其中的内容。


The code I'm using is the following:我正在使用的代码如下:

public function fetchActions() {
    $output = "";
    $actions = glob("actions/*");

    for ($i = 0; $i < count(glob('actions/*')); $i++) {
        $output .= file_get_contents($actions[$i]) . " ";
    }

    return $output;
}

In the file I'm calling this I'm using:在我调用的文件中,我正在使用:

echo eval($methods->fetchActions());

EDIT: The reason why I'm asking is because I heard eval() function shouldn't really be used unless necessary.编辑:我问的原因是因为我听说除非必要,否则不应真正使用 eval() 函数。 They also said if I use the eval() function I should sanitize the code before eval() the code.他们还说如果我使用 eval() 函数,我应该在 eval() 代码之前清理代码。

Never use eval() just use it if there is no other way, but for sure there is other ways.永远不要使用 eval() 只是在没有其他方法的情况下使用它,但肯定有其他方法。 You have to break your head but is better than someone breaks your code and security.你必须打破你的头脑,但总比有人破坏你的代码和安全要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM