简体   繁体   中英

echo php code by php; is that possible

I want to echo out the php code itself for educational purposes. Is this possible? At the same time I also need the code as php code to be executed.

adding html special-chars to this:

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

it becomes:

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

But this still does not output the code itself as the php interpreter seems to get hold of the code and directly interprets it. htmlspecialchars () has only got an effect on the html code, you then get to see the article in brackets.

I also tried using .html files instead of .php files. But PHP interprets it even if you rename it to .jpeg .

I'm at my wit's end. I would be grateful for any answer. Thanking in advance.

I dont understand, why one would actually consider doing it this way, but it is pretty easy. file_get_content will not run the code, while include will.

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));
}

This will echo

Hello World


 <?php echo 'Hello World';  

for given file:

<?php echo 'Hello World';

'echo' seems to somehow process php-code, even if it is a txt file. So before echoing I did this, the precondition being that the code is saved as .txt:

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

if you do this with a file ending with .php the code seems to be even processed by 'file_get_contents' So you have to use .txt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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