php - 在不使用 eval() 的情况下从数据库记录运行 php 脚本

标签 php mysql parsing eval

我想知道如何在不使用 eval() 的情况下运行存储在数据库中的 php 脚本。

预期流程如下

When user POST a string which contains {{typed_alias }}, the system will search the table whether this alias has record in the alias column.

If yes -> replace what user typed with the correspond script which is stored in the replacement column.

If not -> show the original string including {{ wrong_alias }}

预期结果如下

When user posts
Hello, {{morninggg}}, the current unix time is {{nowTime}}

Array output from db

array

 0 =>
   array
     'ID' => 445
     'alias' => 'morning'
     'replacement' => 'Good morning'
 1 =>
   array
     'ID' => 446
     'alias' => 'nowTime'
     'replacement' => time()
 2 =>
   array
     'ID' => 447
     'alias' => 'tommorowNow'
     'replacement' => time()+86400

Return

Hello, {{morninggg}}, the current unix time is 147855220

现在我已经使用foreach 解决了数据库数组问题,也可以使用str_replace() 将别名替换为脚本。

我用来从数据库中获取数据并进行替换的当前类如下

class replace {
    public $definitions;

    public function setDefinitions($definitions) {
        $this->definitions = $definitions;
    }

    public function tag($input) {
        if($this->definitions && is_array($this->definitions)) {
            foreach ($this->definitions as $definition) {
                if($defintion['alias'] == 'time') {
                    $input = str_replace('{{' . $definition['alias'] . '}}', date('Y-m-d'), $input);
                } else {
                    $input = str_replace('{{' . $definition['alias'] . '}}', $definition['replacement'], $input);
                }
            }
        }
        return $input;
    }
}

当前使用方法

$replace = new replace();
$replace->setDefinitions($tagEngine);
$parsedString = $replace->tag($__input);

//$__input is what user POST to the server

echo $parsedString;

然而,目前的结果如下

Hello, {{morninggg}}, the current unix time is time()

脚本无法在页面运行成功

但是当我像这样手动给出定义时

$definition = array('morning' => 'Good Morning', 'nowTime' => time()); foreach ($definition as $key => $value) $source = str_replace('{{' . $key . '}}', $value, $source); return $source;

脚本可以运行并返回

Hello, {{morninggg}}, the current unix time is 147855220

我知道使用 eval() 可以运行脚本,但是,人们认为它在实际应用程序中是一种危险的方法。

谁能给我建议如何处理这个问题?

谢谢!

最佳答案

你不应该使用像 eval() 这样的函数来解决这个问题。您应该从数据库中提取所有 php 代码并按如下方式解析不同的别名(我刚刚更改了 replace 类中的 tag() 方法:

public function tag($input) {
    if($this->definitions && is_array($this->definitions)) {
        foreach ($this->definitions as $definition) {
            $replacement = $definition['replacement'];
            switch($definition['alias']) {
                case 'nowTime':
                    $replacement = date('Y-m-d');
                    break;
                case 'tommorowNow':
                    $replacement = date('Y-m-d', (time() + 86400));
                    break;
            }
            $input = str_replace('{{' . $definition['alias'] . '}}', $replacement, $input);
        }
    }
    return $input;
}

如您所见,对于每个 php 代码别名,您可以在 switch() 语句中添加另一个 case。您可以在以下链接中阅读有关 switch() 控制结构的内容:

PHP: switch - Manual

关于php - 在不使用 eval() 的情况下从数据库记录运行 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27316742/

相关文章:

php - 有没有更简洁的写法 "$a ? $a : $b"?

javascript - 中止之前的ajax请求但有延迟吗?

php - mysql_real_escape_string 和特殊字符

php - Android MySQL 服务器请求类中 Asynctask 的问题

c - 我如何实现解析?

php - 在 php Laravel 5 中创建自定义帮助函数的最佳实践是什么?

php - 按订单选择语句

c++ - 为什么从 std::istream 读取记录结构字段失败,我该如何解决?

parsing - 如何使用 NLP 解析食谱成分?

php - 通过 PHP 保存 png 时获取深色图像