Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

how to execute php from file get contents

file_get_contents php includes echo

     

easypro

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

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

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



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

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

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



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

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

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

7:00 pm on Jul 9, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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

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

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



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

Glad you got it working and welcome to the forums!

- John

phranque

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

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



welcome to WebmasterWorld, easypro!

swa66

9:58 am on Jul 10, 2013 (gmt 0)

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



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

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

8:26 pm on Jul 12, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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

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

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



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

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

WebmasterWorld Senior Member 10+ Year Member



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


You never break out of PHP?

Readie

9:23 pm on Jul 26, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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

10:26 pm on Jul 26, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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

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

10+ Year Member



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month