Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include a php file from within a php file

Status
Not open for further replies.

bdina

Programmer
Jun 17, 2003
47
0
0
US
I am trying to call a php script from within a php script, with limited success currently. I need to have the php script run from within a a php page and have the output display from within the top-level php, what I do currently is a call to readfile which displays the html portion correctly however the php is not parsed, so I just get the php code output along with the other HTML stuff.

The top-level php outputs a page with a content area, inside the content area depending on what the user clicks will display different content. The lower-level php file will create and output the correct content, but as I said earlier, the content window only displays the php code and nothing else (using the readfile()). I need the readfile() to be parsed as php but it is not, for example the lower-level php may build a form to take user input and insert it to a database, the form may be displayed using an HTML table, where we use php to populate a combo-box. With the current setup the HTML form is shown, and the combo box shows the actual php code and not the output from the php code.... my code is a bit complex as I am using a couple classes to build the page, so I will only post the code if it is necessary.

thanks!
--Bryan
 
Then why not use

include("content.php");
or
require("content.php");

Readfile will do just that, read the file in and not evaluate it...

You can try the eval($file); command to get the php to run

Bastien

Cat, the other other white meat
 
There are a few conecpts to keep straight here:

include or require used on a file level will load the PHP code as plain text in the calling file. It is meant to load modules or functions that are distributed over several files.

When you want to have the result of a PHP script displayed then the code needs to be processed by the PHP interpreter first. There are several ways to do that:

a. load it through a HTTP request, i.e. the web server send the result of the interpreted code. (cURL, fsockopen etc., file() with URL wrappers etc.)
b. read the code into a string fread() and use eval()
c. wrap the content of the file in a function and return the output.
 
require() is important to use if the _required_ file is very important.. like a login script to your admin page.

If the required page is not there, the require() will kill further parsing of the page.
If the required page is not there, and you use the include(), it might display your admin page, even if there is an error in your login page!

For simple CMS (flatfile), you do not need to use readfile().

First of all, you need to check that there are no slashes in the variable, which controls what to be included.. (or it might be abused to get passwd file, etc).
then you check if the file exists, with the function is_file()

After that is done, if no errors have arrised, you simply include or require it.

example:
Code:
<a href="?goto=home" title="home">Home</a> | <a href="?goto=information" title="information about us">Information</a><hr />
<?php

IF(!$main) // if not set
  { 
    $main = "Home"; // set default
  } 
if(!stristr($main, "/")) // if no slashes
  { 
    if (is_file($main . ".php")) // if file is there
      {
        include($main . ".php"); // include the file
      }
    else // file missing
      {
        echo "404 - Not found";
      }
  } // end if !stristr($main...
else // slash in the var, most likely someone trying to spy on your server..
  {
    echo "What do you think you are doing?";
  } 
?>

the smartest thing is actually to just show the 404 also when there is a slash in the url! (security issues)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top