bomax
asked on
Passing variables: One PHP Document Within Another
I have a website setup as follows: There is the main file, called index.php. It's job is to show the html to make my page look pretty, which basically includes making up a table. Since I have several pages within my site, I set it up so that to move to a new page the link would be like:
index.php?id=newpage.htm
What happens is index.php uses FOPEN() on 'newpage.htm', reads the contents of the file, then ECHOs the entire contents out in one of the cells of the table. The problem here is that if I want to open up another PHP document that has a form, I can't pass variables to the second page (i.e. 'form.php').
Here is the code:
<?php
if($id) {
$filename=$id;
if(file_exists($filename)) {
$resource = fopen($filename,"r");
$contents = fread($resource, filesize($filename));
$handle = fclose($resource);
} else {
$contents = "Sorry! This area of the site is currently undergoing construction but should be up shortly. Thank you!";
}
} else {
$filename = "primary.htm";
$resource = fopen($filename,"r");
$contents = fread($resource, filesize($filename));
$handle = fclose($resource);
}
?>
<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align=center>
<tr>
<td width="155" background="images/left.jp g"><img src="images/code.jpg" width="155" height="129"></td>
<td width="595" align="left" valign="top" background="images/bluefad e.jpg"><im g src="images/topmenu.gif" border="0" align="baseline" usemap="#Map"></td>
***REMOVED TO SAVE SPACE***
<td background="images/bluefad e.jpg" valign=top><font size = "2" face="Arial, Helvetica, sans-serif"> ***HERE IS WHERE THE FILE IS INSERTED*** <?php printf("%s", $contents); ?></font><br><br><br></td>
</tr>
</table>
<table width="755" border="0" cellspacing="0" cellpadding="0" align=center>
<tr>
<td><img src="images/bottombar.gif" ></td>
</tr>
</table>
***REMOVED TO SAVE SPACE***
</body>
</html>
How can I set it up so that I can use FOPEN to open another PHP document and have that document pass variables to itself?
Thanks,
Matt
index.php?id=newpage.htm
What happens is index.php uses FOPEN() on 'newpage.htm', reads the contents of the file, then ECHOs the entire contents out in one of the cells of the table. The problem here is that if I want to open up another PHP document that has a form, I can't pass variables to the second page (i.e. 'form.php').
Here is the code:
<?php
if($id) {
$filename=$id;
if(file_exists($filename))
$resource = fopen($filename,"r");
$contents = fread($resource, filesize($filename));
$handle = fclose($resource);
} else {
$contents = "Sorry! This area of the site is currently undergoing construction but should be up shortly. Thank you!";
}
} else {
$filename = "primary.htm";
$resource = fopen($filename,"r");
$contents = fread($resource, filesize($filename));
$handle = fclose($resource);
}
?>
<body>
<table width="750" border="0" cellspacing="0" cellpadding="0" align=center>
<tr>
<td width="155" background="images/left.jp
<td width="595" align="left" valign="top" background="images/bluefad
***REMOVED TO SAVE SPACE***
<td background="images/bluefad
</tr>
</table>
<table width="755" border="0" cellspacing="0" cellpadding="0" align=center>
<tr>
<td><img src="images/bottombar.gif"
</tr>
</table>
***REMOVED TO SAVE SPACE***
</body>
</html>
How can I set it up so that I can use FOPEN to open another PHP document and have that document pass variables to itself?
Thanks,
Matt
ASKER
Ok...Basically I want to create a Header file and a Footer file. BUT, when I use the INCLUDE statement, PHP looks in the hosts include directly, however my files are in my directory. Is there a way to have PHP look in my dir instead of the 'include' dir?
yes just try this include "./file.php";
Harry
Harry
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Is there a way to use the include statement, but if the file isn't found to display a nice error message instead of the standard "warning: mysql...."
<?php
$strURL = $HTTP_GET_VARS["name"];
if (($strURL == null) OR ($strURL == "") OR ($strURL == "home")) {
$strURL="inc/index_body.as p";
} else {
$strURL="inc/".$strURL."_b ody.asp";
}
include($strURL);
?>
Use the above code rather than what you want to do - unless you really like displaying error messages (doesnt seem logical - they shouldnt have invalid URLs should they ?)
The above code checks if the var passed e.g. index.php?name=home
the var checked is name and if it is empty or null (doesnt exist e.g. index.php?name="" or index.php?name= then it displays the main page...
or index.php?name=home for the return home button of course :-) otherwise it will go and get the appropriate include file e.g. index.php?name=registratio n then the code will look in the inc folder for a file named registration_body.asp
TIP OF THE DAY: Leave the files as .asp because they cannot be leeched from your server! - the way .inc, .js, .txt etc can be.
$strURL = $HTTP_GET_VARS["name"];
if (($strURL == null) OR ($strURL == "") OR ($strURL == "home")) {
$strURL="inc/index_body.as
} else {
$strURL="inc/".$strURL."_b
}
include($strURL);
?>
Use the above code rather than what you want to do - unless you really like displaying error messages (doesnt seem logical - they shouldnt have invalid URLs should they ?)
The above code checks if the var passed e.g. index.php?name=home
the var checked is name and if it is empty or null (doesnt exist e.g. index.php?name="" or index.php?name= then it displays the main page...
or index.php?name=home for the return home button of course :-) otherwise it will go and get the appropriate include file e.g. index.php?name=registratio
TIP OF THE DAY: Leave the files as .asp because they cannot be leeched from your server! - the way .inc, .js, .txt etc can be.
hmmm more of an A answer i'd say! :D
ASKER
Thanks... :)
Harry