Echo php code by php; it is possible

I want to express php code itself for educational purposes. Is it possible? At the same time, I also need the code as php code to execute.

adding html special-chars

to this:

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

      

he becomes:

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

      

But that still doesn't output the code itself, as the php interpreter seems to take possession of the code and interpret it directly. htmlspecialchars ()

only affects the html code, then you can see the article in parentheses.

I also tried to use .html

files instead of .php

files. But PHP will interpret it even if you rename it to .jpeg

.

I'm at my end. I would appreciate any answer. Thanking in advance.

+3


source to share


2 answers


I don't understand why it would be possible to do this, but it's pretty easy. file_get_content won't run the code, but 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));
}

      

It will echo



Hello world


<? php echo 'Hello World';

for a given file:

<?php echo 'Hello World';

+3


source


'echo' seems to handle php code somehow, even if it's a txt file. So before repeating this I did this, the premise is 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 in .php the code seems to even be parsed by "file_get_contents". Therefore, you must use .txt.

0


source







All Articles