【问题标题】:echo php code by php; is that possible通过 php 回显 php 代码;那可能吗
【发布时间】:2015-06-13 14:24:31
【问题描述】:

出于教育目的,我想回显 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 也会对其进行解释。

我已经束手无策了。我将不胜感激。提前致谢。

【问题讨论】:

  • file_get_contents 不解释任何 PHP,这里还有其他事情发生。有问题的文件中究竟有什么?
  • 你实际上是如何回显代码的?
  • 只需这样做str_replace('<?php','',$yourdata);
  • $this->Art = file_get_contents("$this->Mainpage/$this->dir/$this->Artikel"); $this->section = explode("End_Embed", $this->Art); $this->Meta_Text=$this->section[0]; $this->content=$this->section[1];echo ("$this->content");
  • 但我会试试 Feroz 的代码。谢谢你。只是试图掌握 php 内部发生的事情。

标签: php


【解决方案1】:

我不明白,为什么要考虑这样做,但这很容易。 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));
}

这会回响

你好世界


对于给定的文件:

&lt;?php echo 'Hello World';

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 它没有。您可能正在下载一个文件(file_get_contents 适用于本地和远程文件),因此您的网络服务器会预处理 php 代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多