NameSilo

Problem with file_get_contents

SpaceshipSpaceship
Namecheap AuctionsNamecheap Auctions
Namecheap AuctionsNamecheap Auctions
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
Close
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
Close
i'll try that
 
0
Close
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
Close
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
Close
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
Close
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
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

New: watch any domain
You can now watch domains on NamePros. We check each one and notify you when something changes. Search for a domain and press Watch on...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back