Jump to content


Photo
- - - - -

[php] - [better Page Including] - [winuu]


  • Please log in to reply
5 replies to this topic

#1 winuu

winuu

    Young Padawan

  • Members
  • Pip
  • 13 posts
  • Location:Finland
  • Interests:Drawing, designing graphics, programming, composing music.

Posted 05 May 2005 - 01:03 PM

Many people use this way to include pages in their PHP files:

<?php
if ($page = "software")
{
  include "software.php";
}
elseif ($page = "moose")
{
  include "moose.php";
}
elseif ($page = "about")
{
  include "about.php";
}
else
{
  include "default.php";
}
?>

The above code can be made simply by effectively using the Switch statement:

<?php
switch ($page)
{
  case "software":
  case "moose":
  case "about":
    include "$page.php";
    break;
  default:
    include "default.php";
    break;
}
?>

Now, if REGISTER_GLOBALS is set off in php.ini, just replace $page with $_GET["page"], and it should work. ^_^

#2 adam123

adam123

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 2,306 posts
  • Location:London, UK
  • Interests:Programming and stuff.

Posted 05 May 2005 - 01:08 PM

They work basically the same, a better way would be:

<?php

if (file_exists($_GET['page'] . ".php"))
{
  include $_GET['page'] . ".php";
}
else
{
  include "404.php";
}

?>

Oh yeah, you shouldn't really use $page instead of $_POST['page']/$_GET['page'] as it poses as a security threat.

#3 MaRmAR

MaRmAR

    Young Padawan

  • Members
  • Pip
  • 18 posts
  • Location:Slovakia

Posted 06 May 2005 - 02:21 AM

if you use
import_request_variables("gp");
somewhere at the start of your PHP, you will not have to use variable format like $_GET["page"]...

#4 winuu

winuu

    Young Padawan

  • Members
  • Pip
  • 13 posts
  • Location:Finland
  • Interests:Drawing, designing graphics, programming, composing music.

Posted 06 May 2005 - 05:13 AM

They work basically the same, a better way would be:

<?php

if (file_exists($_GET['page'] . ".php"))
{
  include $_GET['page'] . ".php";
}
else
{
  include "404.php";
}

?>

Oh yeah, you shouldn't really use $page instead of $_POST['page']/$_GET['page'] as it poses as a security threat.

Actually, that would just be an easier way, not necessarily a better way to do it; the code sample you provided actually has a security flaw. It allows the user to include any page/file on the server. :rolleyes:

#5 adam123

adam123

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 2,306 posts
  • Location:London, UK
  • Interests:Programming and stuff.

Posted 06 May 2005 - 10:37 AM

Yeah, but why the hell would you store important files UNPROTECTED in a directory anyone can view? Users can't view files in .htaccess/.htpass protected pages, they can't view files below the current directory, etc. etc.
I'm not going to get into an argument about this as it's stupid, but i'm just proving my point.

#6 winuu

winuu

    Young Padawan

  • Members
  • Pip
  • 13 posts
  • Location:Finland
  • Interests:Drawing, designing graphics, programming, composing music.

Posted 06 May 2005 - 10:41 AM

:P Never mind...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users