homepage Welcome to WebmasterWorld Guest from 54.204.58.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
how to execute php from file get contents
file_get_contents php includes echo
easypro




msg:4591383
 2:25 am on Jul 9, 2013 (gmt 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!

 

JAB Creations




msg:4591389
 3:12 am on Jul 9, 2013 (gmt 0)

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

coopster




msg:4591550
 5:05 pm on Jul 9, 2013 (gmt 0)

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.

easypro




msg:4591560
 5:58 pm on Jul 9, 2013 (gmt 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.

easypro




msg:4591561
 5:59 pm on Jul 9, 2013 (gmt 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);

Matthew1980




msg:4591587
 7:00 pm on Jul 9, 2013 (gmt 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

JAB Creations




msg:4591708
 5:19 am on Jul 10, 2013 (gmt 0)

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

Glad you got it working and welcome to the forums!

- John

phranque




msg:4591720
 6:08 am on Jul 10, 2013 (gmt 0)

welcome to WebmasterWorld, easypro!

swa66




msg:4591754
 9:58 am on Jul 10, 2013 (gmt 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.

easypro




msg:4591812
 2:00 pm on Jul 10, 2013 (gmt 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.

Readie




msg:4592585
 8:26 pm on Jul 12, 2013 (gmt 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.

JAB Creations




msg:4592679
 6:49 am on Jul 13, 2013 (gmt 0)

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

eeek




msg:4595469
 7:59 pm on Jul 22, 2013 (gmt 0)

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


You never break out of PHP?

Readie




msg:4596855
 9:23 pm on Jul 26, 2013 (gmt 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".

JD_Toims




msg:4596869
 10:26 pm on Jul 26, 2013 (gmt 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)

Dinkar




msg:4597158
 10:50 am on Jul 28, 2013 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved