Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo php code by php; is that possible

Tags:

php

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.

like image 701
PoohBear Avatar asked Sep 19 '26 23:09

PoohBear


1 Answers

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';

like image 115
manf Avatar answered Sep 21 '26 13:09

manf