Avatar of bomax
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.jpg"><img src="images/code.jpg" width="155" height="129"></td>
    <td width="595" align="left" valign="top" background="images/bluefade.jpg"><img src="images/topmenu.gif" border="0" align="baseline" usemap="#Map"></td>
***REMOVED TO SAVE SPACE***
    <td background="images/bluefade.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
PHP

Avatar of undefined
Last Comment
bomax

8/22/2022 - Mon
harwantgrewal

Not able to get your point. Are you saying if the include file will have some vairables how you will get them. Pleas explain more


Harry
bomax

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?
harwantgrewal

yes just try this include "./file.php";

Harry
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Devastated

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bomax

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...."
Devastated

<?php
$strURL = $HTTP_GET_VARS["name"];
if (($strURL == null) OR ($strURL == "") OR ($strURL == "home")) {
     $strURL="inc/index_body.asp";
} else {
     $strURL="inc/".$strURL."_body.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=registration 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.
Devastated

hmmm more of an A answer i'd say! :D
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bomax

ASKER
Thanks... :)