Page 1 of 1

Dynamic Navigation

Posted: Sat Jun 24, 2006 10:27 am
by tail
I'm trying to make my links like this:
http://perks.mypimpedlayout.com/index.p ... e=schedule

Where 'id' selects the folder and 'page' selects the file inside that folder. I'm using this code but it isn't working:

Code: Select all

<?php
$id = $HTTP_GET_VARS["id"];
$page = $HTTP_GET_VARS["page"];
$extension = "php";
if ( !$id || $id == "" )
{include "./school.php";}
else if ( file_exists( "$id.$extension.$page" ) )
{include "$id.$extension.$page";}
else{ echo "<center><h2>404 ERROR!</h2></center>"; }
?>

Posted: Sat Jun 24, 2006 3:00 pm
by phpCCore Brad
Are you trying to make a link IE:

Code: Select all

<a href="viewpage.php?id=school&page=schedule">Link</a>
Then when the link is clicked it loads the page:

school.php.schedule

?

Re: Dynamic Navigation

Posted: Sat Jun 24, 2006 3:04 pm
by sheila
This should work. You need to be careful and filter $page since it is user-supplied input.
I've added basename() which is just basic filtering.
Also, use $_GET instead of $HTTP_GET_VARS

Code: Select all

<?php
$id = $_GET["id"];
$page = $_GET["page"];
$extension = "php";
// strip any path that has been sent as part of $page
$page = basename($page);
$file = "$id/$page.$extension";
if ( !$id || $id == "" )
{ include "./school.php"; }
else if ( file_exists( $file ) )
{ include $file; } 
else { echo "<center><h2>404 ERROR!</h2></center>"; }
?>

Posted: Sat Jun 24, 2006 5:26 pm
by tail
That worked, thanks.

Posted: Sat Jun 24, 2006 5:33 pm
by Charles256
erm. I recommend using a switch statement and each case checks for expected inputs and includes the appropriate page, if no match then take them to the home page...

Posted: Sat Jun 24, 2006 6:28 pm
by tail
It's the same thing but instead of taking them to the home page it says 404 ERROR!

Posted: Sat Jun 24, 2006 6:40 pm
by Charles256
erm.what is?show some code.