NameSilo

Problem with file_get_contents

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Barrucadu

Established Member
Impact
64
Here is my code:

PHP:
if(isset($_GET['page'])){
	$page = $_GET['page'];
}else{
	$page = 'main';
}

$contents = file_get_contents($page.'.php');

Atatched is a screenshot of what happens, anyone got any ideas? For some reason, only the first few lines of php in the files are being parsed.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
You are showing the contents of the file, not actually running the script.

Use include() if that's what you're going to do. (Clean up $_GET['page'] to stop hacking attempts.)
 
0
•••
i'll try that
 
0
•••
Yeah use include or require.
file_get_contents is good when you want to read a file into a variable like a template to send E-mail etc.
 
0
•••
The thing is, the code in the included file may redirect the user to another page, then again, it may not, so I cant include it where I want it to display, because that would make the header() redirect not work, and i cant include it at the top of the code because it would display in the wrong place.
I've found a soloution though, if I store the pages HTML contents in a variable, I can use a combination of include() and return() to do the job.
 
0
•••
Alright.. file_get_contents.. Get the entire file.. So you need to echo it afterwards.. If that does not work.. I suggest giving file a try.

It looks like the PHP is not being noted as PHP and just printing the file contents.. Could be a number of reasons. Make sure you use " insted of ' and make sure you echo the contents.

I see you got it fixed..

- Steve
 
0
•••
I've found a better method:

PHP:
<?php ob_start(); ?>
FIRST HALF OF HTML GOES HERE
<?php include 'page.php'; ?>
SECOND HALF OF HTML GOES HERE
<?php ob_get_flush(); ?>

ob_start() stores all the html in a buffer, but sends headers, and ob_get_flush sends all the html. Perfect for when my included file contains header()'s
 
0
•••
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Escrowly
CryptoExchange.com
Domain Recover

NamePros updates

Parking: find your domains by pasting a list
You can now paste a list of domain names into the search box at the top of the Parking menu. Put one name on each line. Your domain list...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back