Welcome to WebmasterWorld Guest from 18.205.109.82

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

how to execute php from file get contents

file_get_contents php includes echo

     
2:25 am on Jul 9, 2013 (gmt 0)

New User

5+ Year Member

joined:July 9, 2013
posts: 4
votes: 0


I have a php page (page1.php) that loads another php page (page2.php).
with page2.php I have other php codes and "includes" that I want to work.
I use content=file_get_contents(file_path/page2.php) to get the code from page2.php.
I then use str_replace() to change the file path of the "includes()" on page2.php so that they are relative to page1.php
then I echo my content but it does not show up.
when I view the source code, it's all there and correct, but it's not rendering or executing or whatever on the page.

Here is some sample code form page2.php:
<div id="header"><? include("pages/header.php"); ?></div>

Here is my code on page1.php:
$Content = file_get_contents("../folder/page2.php");
$Content = str_replace('pages/','../folder/pages/',$Content);
- this fixes my file path from page1.php to header.php
echo $Content;

I have been at this all day and google has not helped.
somebody has to know how to do this.
please help!
3:12 am on July 9, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 26, 2004
posts: 3178
votes: 22


You're looking to use eval [php.net] however there are greater concerns using it at a server than at a client so do some extra research and make sure you absolutely trust where that code is coming from.

- John
5:05 pm on July 9, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


Welcome to WebmasterWorld, easypro.

Also, rather than use file_get_contents() you can just include() page2.php instead. And if you need to manipulate the contents you can use output buffering to store the data in a variable first using ob_start() and friends.
5:58 pm on July 9, 2013 (gmt 0)

New User

5+ Year Member

joined:July 9, 2013
posts: 4
votes: 0


thanks for all the great info guys! I actually did try using include() and ob_start() but couldn't get it to work right. I probably just don't know what I'm doing since I have never used ob_start() before.
also, I don't really see how eval() is any more risky than include() - either way the code is being executed, so if there was malicious code being loaded, it executes both ways.
or am I missing something? sorry, I'm still an intermediate coder.
5:59 pm on July 9, 2013 (gmt 0)

New User

5+ Year Member

joined:July 9, 2013
posts: 4
votes: 0


I actually got it to work by using this:
$Content = file_get_contents("../folder/page2.php");
$Content = str_replace('pages/','../folder/pages/',$Content);
$Content = "?> ".$Content;
eval($Content);
7:00 pm on July 9, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2009
posts:1397
votes: 0


HI there easypro,

A warm welcome to WebmasterWorld!

>><div id="header"><? include("pages/header.php"); ?></div>

This is poor coding, please declare that you are actually using php so that the parser can read what it is meant to!

Should be

<div id="header"><?php include("pages/header.php"); php?></div>

Cheers,
MRb
5:19 am on July 10, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 26, 2004
posts: 3178
votes: 22


Matthew1980 is correct, you should use the full PHP declaration.

Glad you got it working and welcome to the forums!

- John
6:08 am on July 10, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11824
votes: 237


welcome to WebmasterWorld, easypro!
9:58 am on July 10, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
posts:4783
votes: 0


As far as I can tell you're supposed to use
When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them.

Ref: [php.net...]
I believe the php?> closing to be in error.
2:00 pm on July 10, 2013 (gmt 0)

New User

5+ Year Member

joined:July 9, 2013
posts: 4
votes: 0


I used "<?php" when I first started coding years ago, but then just changed it to the shorter version "<?" and I have never had a problems with it in the past 4 years. Kinda like CSS when you use #000 instead of #000000.
8:26 pm on July 12, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:945
votes: 0


The problem with using <? is there's a flag for it in the php.ini file which your server administrator needs to turn on for it to work. While your code works on your current box, if you change hosts or anything of the like, your code could just stop working.

For the sake of 3 additional keypresses each time you start a new file, you keep your code much more portable.
6:49 am on July 13, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 26, 2004
posts: 3178
votes: 22


Get a copy of Advanced Find and Replace, yeah it's $20 but I use it every day I code. Find "<?" and replace with "<?php" and it'll save you hours of time. While I suppose "<?" could be called shorthand it really doesn't apply to PHP like it does CSS.

The stricter your code the better and much more responsive you'll become. Turn up error-reporting to it's maximum setting in PHP...

<?php
error_reporting(E_ALL);
?>

...and make sure you address all errors including when variables aren't set and are referred to.

Create an .xhtml file on your desktop (from a Notepad template) and open it in Firefox: broken XML. You can code strict or loose, and strict might take a little longer to learn though you'll be so much more capable in the long term.

- John
7:59 pm on July 22, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 20, 2004
posts:882
votes: 2


For the sake of 3 additional keypresses each time you start a new file


You never break out of PHP?
9:23 pm on July 26, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:945
votes: 0


You never break out of PHP?

I like to split my pages into a PHP script and a template file. In the PHP file I almost never break PHP. In fairness though, there's a lot of things like this in the templates:

<div class="head">
<h2><?php echo utils::pageReadyString($page->content['title']); ?></h2>
</div>

But those templates are often shared between more than one type of page. E.g., news and information pages both share the same template file as they both have a title, a sub-navigation menu and a body, and I made sure they use the same variable names. News then just includes another template for user comments. Makes it easier to maintain a consistent feel through the site.

So the answer to your question is: "it depends" :)

But yes, point taken, I should have said "for the sake of 3 additional keypresses each time you enter PHP".
10:26 pm on July 26, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month

joined:July 19, 2013
posts:1097
votes: 0


You never break out of PHP?


I don't if I want/need "flat out fast" processing speed... Check out the PHP Benchmarking Results [webmasterworld.com] in the Library. (Concatenation, accumulation, single quotes, single echo = screamin fast)
10:50 am on July 28, 2013 (gmt 0)

Full Member

10+ Year Member

joined:July 12, 2002
posts:207
votes: 1


It's good that it's working for you. But I don't recommend you to use eval as I don't see any need.

I like simple code. You can put all that content of php2 in a function and pass the value when calling it. In that function accept the value in a variable and use that variable where you want to use.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members