Dynamic Navigation

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
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Dynamic Navigation

Post 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>"; }
?>
phpCCore Brad
Forum Commoner
Posts: 47
Joined: Sun Dec 04, 2005 5:46 pm
Location: Michigan, USA
Contact:

Post 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

?
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Re: Dynamic Navigation

Post 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>"; }
?>
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

That worked, thanks.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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...
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

It's the same thing but instead of taking them to the home page it says 404 ERROR!
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

erm.what is?show some code.
Post Reply