Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Passing variables: One PHP Document Within Another

Posted on 2003-02-28
8
Medium Priority
?
225 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:bomax
  • 3
  • 3
  • 2
8 Comments
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 8046837
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
0
 

Author Comment

by:bomax
ID: 8046856
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?
0
 
LVL 5

Expert Comment

by:harwantgrewal
ID: 8046928
yes just try this include "./file.php";

Harry
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Accepted Solution

by:
Devastated earned 300 total points
ID: 8047137
You don't need to read a files contents and display it to the page... you simply use an inclde statement as follows...


<?php include("inc/footer.txt"); ?>

in the above example..

if the code above were in index.php then in the same folder there must be a folder named inc and in there the footer.


P.S. You decide where it looks.
P.P.S. You may have some problems if you start including files within includes etc etc but most can be done - u just have to work out the paths correctly.
0
 

Author Comment

by:bomax
ID: 8047174
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...."
0
 
LVL 4

Expert Comment

by:Devastated
ID: 8047321
<?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.
0
 
LVL 4

Expert Comment

by:Devastated
ID: 8047324
hmmm more of an A answer i'd say! :D
0
 

Author Comment

by:bomax
ID: 8048332
Thanks... :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question