【问题标题】:How to execute text as PHP如何将文本作为 PHP 执行
【发布时间】:2012-05-27 01:04:49
【问题描述】:

我有一个问题,我想像 PHP 一样抓取文本并执行文本,但是我该怎么做呢?例如,我在 .txt 文件中有此代码:

$tweetcpitems->post('statuses/update', array('status' => wordFilter("The item Blue has             been released on Club Penguin.")));
$tweetcpitems->post('statuses/update', array('status' => wordFilter("The item Green has been     released on Club Penguin.")));

现在的问题是我抓取了这个文本,我想将它作为 PHP 脚本执行,我该怎么做?请帮忙!

【问题讨论】:

  • 我希望您知道,如果您通过用户输入执行此操作,这是一个被黑客入侵的邀请?
  • 如果你问如何使用eval,你应该绝对不要使用eval

标签: php scripting execute


【解决方案1】:

【讨论】:

  • 附注:eval() + file_get_contents()include 基本相同。如果您要这样做,您不妨将<?php 添加到文件的开头并使用include
【解决方案2】:

您可以从同一个脚本同时运行 text 和 eval,但就像前面提到的那样。安全必须非常严格。不过,如果使用得当,eval 功能确实很强大。试试下面的代码。

$b = 123;
$a = "hello <?php echo 'meeeee'; ?>. I just passed $b from the mother script. Now I will pass a value back to the mother script" . '<?php $c; $c = 1 + 8; ?>' .
     "I was call within a function, therefore my variable can't passed to the global script. Nonetheless, let try something globally" .
     "<?php 
        global \$d;
        \$d = 'I am now a global var. Take care though, don\\'t let anyone edit your file' ;
      ";

function parseTxtAsCode($invalue){
    if( !is_string($invalue) ) return false;
    eval('?>' . $invalue);
    echo "\n\n\n\n Can't believe I got this from my child: $c \n";
}

parseTxtAsCode($a);
echo "\n\n\n I am global and this is what I got from the eval function: $d";

【讨论】:

    【解决方案3】:

    您可以使用eval 将文本评估为PHP;但是,请阅读下面的非常重要的免责声明!

    eval() 语言结构非常危险,因为它允许执行任意 PHP 代码。因此不鼓励使用它。如果您已仔细验证除了使用此构造别无其他选择,请特别注意不要将任何用户提供的数据传入其中,而无需事先正确验证。

    // $result is a string containing PHP code. Be sure you trust the source of
     // the PHP code prior to running it!
    eval( $result );
    

    【讨论】:

      【解决方案4】:

      您可以使用 include() 或 require() 函数:

      include(/your_text_file.txt);
      
      // OR
      require(/your_text_file.txt);

      或 Eval 函数(如第一条评论)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 2010-11-26
        • 2010-09-16
        • 2016-05-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多