How do I get PHP "includes" in an echoed string "executed"?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

How do I get PHP "includes" in an echoed string "executed"?

Post by BobN »

I have a .PHP file with PHP includes embedded in it to include some HTML into the page, such as the menu and the page diversions layout. I use SSI when the file is not as .PHP file.

If make up a little test and my php code does the following:

Code: Select all

ob_clean();  // to get rid of any earlier output.
include("file.php");
the PHP includes in the included file are "executed/evaluated/performed" - whatever you wish to call it.

If I do it as follows, the includes are not "executed.'

Code: Select all

ob_clean(); 
$fileContents = get_file_contents("file.php");
echo $fileContents;
Any thoughts?

Bob
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: How do I get PHP "includes" in an echoed string "execute

Post by twinedev »

include/require take the specified file and processes it as if the code in the file was right there in the file that was calling it.

When you do get_file_contents() you are just reading the contents of the file into a variable.

*IF* you must, then you would need to do eval() on the contents of the file (see http://php.net/eval ) however, you should do this as little as possible, Eval eats up a lot of processing resources. Also if the code you are eval'ing is possibly edited by the site visitor, you open up a big security issue.

-Greg
BobN
Forum Newbie
Posts: 8
Joined: Fri Feb 03, 2012 1:03 am

Re: How do I get PHP "includes" in an echoed string "execute

Post by BobN »

twinedev,

Thanks for the reply.

I figured that the include was processing the file and just echoing the contents out would not be the same but I was hoping that there might be another way to do it besides using eval.

I have used it in JavaScript but quite sparingly and only when the alternative was even worse.

I would not want to run this whole file through such a process, as you say, it isn't exactly the most efficiency process around.

Well, I think I'll put the issue on a back burner and see what pops up after it has been simmering for a while.

Ah, I thought of something earlier but did not have time to check on it - is there a way to tell the server software to process SSIs in a .php file? And, would it happen if I echoed the file contents instead of including it.

Most of the includes I'm looking at are SSIs in non PHP files - they create the page layout I'm using across the site (in the new version I'm working on). In the PHP files I've created so far, I've inserted PHP includes to do the same thing as the SSIs in the .shtml files. If I could get the server to handle the SSIs when I echo the contents of a file out, it would solve my problem - pretty much solve it, anyway.

I know there's an entry in the .htaccess file that might do it and I read about some sort of twick where you could get the sever to handle SSIs if you set the file permission correctly, I seem to think it was setting the file as executable.

One last thing, I have the free Abyss server installed on my system to allow me to test without having to upload to a server. I installed PHP support and everything works just fine - excerpt for reCAPTCHA validation - but I'm going to dump that anyway -- just too much of a burden on visitors. I've going to try a few other ways to weed out the spam.

I highly recommend the Abyss server with PHP support installed as a testbed for PHP code. http://www.aprelium.com/

Bob

Oh, here goes, I've got to enter that damn CAPTCHA
Post Reply