Problem with include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Problem with include

Post by Getran »

Hey ppl, i got a script that grabs a file and prints it. I made a post about displaying the contents of a file before, but i needed to use PHP in the files. Anyway, i changed the bit that opened it to just include(), but ran into a little problem. When it includes the page, it doesn't display it where i want it to... this is the script:

Code: Select all

if (isset($_REQUEST['page']))
{
  $inc = "pages/".$_REQUEST['page'].".php";
  if (!file_exists($inc))
  {
    $content = "Page not found";
  }
  else
  {
    $content = include($inc);
  }
}
else
{
    $content = include("pages/main.php");
}
It displays it at the top of the page, but i have another include in there to display navigation.php, which works fine..(in the correct place). Where i want that to display i just have echo $content;.

Also a weird thing i noticed, where the content should be displayed, it says "1" and nothing else.

Can someone help me with this ?

Thx in advance
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

echo include($page);

will return 1 if succesful 0 is not

if you want to contents of the page look into [php_man]file_get_contents[/php_man] ,[php_man]fopen[/php_man]
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

nvm, i sorted it a different way
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you should share your answers, so other ppl with the same problems can search :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

always nice when people send stuff like

?page=../index etc ;)
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

oh sorry :roll:

I changed the bit i posted first to this:

Code: Select all

if (isset($_REQUEST['page']))
{
  $inc = "pages/".$_REQUEST['page'].".php";
  if (!file_exists($inc))
  {
    $content = "Page not found";
  }
  else
  {
    $method = "INCLUDEFILE";
    $content = "";
  }
}
else
{
    $method = "INCLUDEMAIN";
    $content = "";
}
Then changed echo $content; to:

Code: Select all

if ($method == "INCLUDEFILE")
{
  include($inc);
}
else if ($method == "INCLUDEMAIN")
{
  include("pages/main.php");
}
else
{
  echo $content;
}
Have fun lol
Post Reply