Saving HTML output from a local PHP file to a string using file_get_contents

There is a header.php file and contains some php codes that return HTML. I know that I can use require, include for echoing results, but I want to save the processed output string to a variable.

On the page, I used:

$headerHTML=file_get_contents('header.php'); 

Then I got the output of the PHP code, not the processed HTML output. I know adding http: // will help. But I prefer to use a relative path, how can I say that the function correctly processes the php file?

Note. I would like to continue to use this file_get_contents statement, rather than using ob_start() if possible.

+4
source share
4 answers

I would rather use require() wrapped inside ob_start() and ob_get_clean() . I am sure there is nothing wrong with this approach.

+5
source

Do not use eval() - this is evil!

Use a relative local path to automatically map it to an absolute URL .

+1
source

If the URL wrappers are included and you want to get the header.php file (and you do not want to save the session state), you can use $headerHTML=file_get_contents('http://yourdomain.tld/path/to/header.php'); although why would you like to do this, it eludes me. Are you sure you are not trying to do something that can be easily solved using templates and caching?

+1
source

You can check out http://in2.php.net/manual/en/function.eval.php#56641 , hope this helps.

0
source

Source: https://habr.com/ru/post/1308724/


All Articles