views:

164

answers:

2

For my website I want to store the general format of the site in a single PHP file in a single location, and each of the different pages content in the local location of the page. I then want to pass the title and content address to the included file via a variable.

However I can't get the included format file to read the variables storing the title and content data.

AKA, the called file for the individual page would be:

<?php
$title = 'Some Title';
$source_file = 'content.php';
readfile('http:...../format.php');
?>

The format file would be:

<html> ...
<title>
<?php
echo $title;
?>
</title>
...
<?php
include($source_file);
?>
...

I recall reading somewhere I need to include something to get the variables at the start of the format file, however I can't remember what it is or find where I found that information.

+5  A: 

If you mean to use include() use include() not readfile() which is for outputting the actual contents of a file... readfile() won't parse PHP code at all, so there is no way to pass variables to it.

The reason you are seeing PHP code being executed is that your web server is actually making a second HTTP request (to itself?), and running a second process to execute your PHP. This second process will not have the same session, nor will it have access to any cookies, or other variables from the first process.

You could pass some variables along using the GET parameters, but if the file you want to include is on the same server, you should just use include(). If the file you want to include is on another server, you should stop and think about what you are doing because you are asking for trouble.

gnarf
Clarification: If you are using readfile() to load a http:// url, then the resulting page will already be parsed with php (that is - it's a string containing html code)
Joel L
I have tried using the include instead of read file, but the hosting services hates it and doesn't load the required file.Strangely the readfile() does load up the PHP code and executes it properly, unless the file is in the same location as the calling file. It is very confusing behaviour!
Alpha1
@Alpha1 hosting services doesn't hate anything. It isn't human. It's a program. You have just experienced an error of some sort, and you have to repair it, not doing another way, backasswards one.
Col. Shrapnel
A: 

You need to pass parameters explicitly.

<?php
$title = 'Some Title';
$source_file = 'content.php';
readfile('http:...../format.php?title='.urlencode($title).'&source_file='.urlencode($source_file));
?>

.

<html> ...
<title>
<?php
echo $_GET['title'];
?>
</title>
...
<?php
include($_GET['source_file']); // Don't do that! 
                               // Never use include with argument that came from outside
?>
...

Please DON'T use include() with parameter that came from outside (as shown above). This is a gaping security hole.

Kamil Szot
`s/potential/a gaping/`
gnarf
Cool .. a web app that p0wns itself!
Tim Post
You all are right. I stated danger of such solution too mildly. Now corrected.
Kamil Szot
For future reference - this is a horrible "solution"
Joel L
To downvoters: If you know how can I stress more dangers of this solution to future vieweres please let me know. I won't delete this answer because it adressess the question asked. However misguided the question itself is.
Kamil Szot
I would still remove it. The question author is - for whatever reason - unable to use include() properly, and using a HTTP request is definitely *not* a solution.
Joel L
I don't agree. I can imagine perfectly valid scenario of using readfile() to access some other page on different location. The trouble is just this include() with argument that came from outside.
Kamil Szot
Quite strange answer, contains only bad code and full of warnings but not a sign of right solution. I don't think answers like `Shoot yourself in a leg. Don't do that! That's dangerous!!!` are ever sensible.
Col. Shrapnel
Author of the question asks how he can make a hole in a coin that he has under his foot using a gun. I explain how he can do it in this arrangement, warn him that he will also get a hole in his foot during this process and advise him not to do it. If you want to give him better answer please explain to him step by step how to move his leg. I'll be the first to upvote it.
Kamil Szot
I did not write this answer with evil intent. I just wanted to answer question within boundaries stated. I was little careless and I gave dangerous idea probably to someone who has no capacity yet to fully understand how dangerous it is and I'm sorry for that, but I see no point in removing the answer now. Let it serve as a warning to future viewers.
Kamil Szot