通过php回复php代码;那可能吗

时间:2015-06-13 14:24:31

标签: php

我想回忆一下php代码本身用于教育目的。这可能吗?同时我还需要代码作为php代码来执行。

在此处添加html special-chars

$this->art = file_get_contents("$this->Mainpage/$this->dir/$this->article");

它变成了:

$this->Art = htmlspecialchars( file_get_contents("$this->Mainpage/$this->dir/$this->Artikel"));

但是这仍然没有输出代码本身,因为php解释器似乎掌握了代码并直接解释它。 htmlspecialchars ()只对html代码产生影响,然后您可以在括号中看到该文章。

我还尝试使用.html个文件而不是.php个文件。但即使您将其重命名为.jpeg,PHP也会对其进行解释。

我的智慧结束了。我会很感激任何答案。提前感谢。

2 个答案:

答案 0 :(得分:3)

我不明白,为什么人们会考虑这样做,但这很容易。 file_get_content不会运行代码,而include会。

define('DS', DIRECTORY_SEPARATOR);

// Logic starts here
public function main() {
    echo '<hr><pre>' . $this->educlude($this->Mainpage . DS. $this->dir . DS . $this->Artikle) . '</pre>';
}

public function educlude($file) {
    include $file;
    return htmlspecialchars(file_get_contents($file));
}

这将回显

  

Hello World


<?php echo 'Hello World';

给定文件:

<?php echo 'Hello World';

答案 1 :(得分:0)

'echo'似乎以某种方式处理php代码,即使它是一个txt文件。所以在回复之前我做了这个,前提条件是代码保存为.txt:

$this->Art =nl2br (htmlspecialchars(file_get_contents("$this->Mainpage/$this->dir/$this->Artikel"))); 

如果您使用以.php结尾的文件执行此操作,则代码似乎甚至由'file_get_contents'处理,因此您必须使用.txt。