PDA

View Full Version : Passing URL Variables Using PHP Include


Nick
2005.11.20, 10:38 PM
For whatever reason I can't get this to work and can't find anything about it using Google. Can anyone help me out? For some reason, when I try to pass the URL variable, it doesn't include the file at all. If I take off the URL variable it does work.


//(from index.php)
<? include("header.php?id=home"); ?>

//(header.php)
<?
$pretext = "";
if($id != "home") {
$pretext = "../";
}
?>
<div class="header">
<h2><a href="<? echo $pretext; ?>">SimReality Studios</a></h2>
<a href="<? echo $pretext; ?>music/">Music</a>
<a href="<? echo $pretext; ?>3d_graphics/">3d Graphics</a>
<a href="<? echo $pretext; ?>games/">Games</a>
</div>

Steven
2005.11.21, 12:18 AM
I believe when you include a file the variables in the current context are carried over - what you're doing is asking the PHP interpreter to include a file called "header.php?id=home" which obviously doesn't exist. PHP doesn't parse URLs in the include as far as I know.

Instead, set a variable in the current block before you call include. If that doesn't work, try global variables or something.

Nick
2005.11.21, 10:15 AM
Thanks. I didn't know included scripts could access variables that were defined in the calling file. That worked great.