Help - Search - Members - Calendar
Full Version: Simple and rapid navigation system
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
cigraphics
I've made my own navigation system:
CODE
<?php
if($id == NULL) {
include('home.php');
} else {
include(''.$id.'.php');
}
?>


let's break the code a little:
CODE
if($id == NULL)

If the $ID has no ?id=test or something it will include the home.php file
CODE
include('home.php');


else it will include the ?id=download and the file will be download.php
CODE
include(''.$id.'.php');
NinJA999
First, you would have to do
CODE
$id=$_GET['id'];

to use the id variable.
Then, you could use this code to actually go to the pages rather than including them.
CODE
<?php
$id=$_GET['id'];
if($id == NULL) {
header("Location: home.php");
} else {
$theplace=$id+".php";
header("Location: $theplace");
}
?>
Matthew.
Don't bother with using IF statements for navigation,

Simple use switch for something simple.
rus321
CODE
switch($_GET['id']) {

case 'home':
include("index.php");
break;

default:
include("404.php");
}
cigraphics
i know to do it with switch statement but i prefered to make my own
Indigo
Before including files, you could also check if the file exists:

CODE
<?php (file_exists(file.php)) ? include "file.php" : include "error.php"; ?>


To check if the id is a number, use something like is_numeric (don't remeber the exact name, but it might be something like that).
Hit3k
CODE
<?php
if($id == NULL) {
include('home.php');
} else {
include(''.$id.'.php');
}
?>

That could be used for XSS (Cross Site Scripting) exploit
eg
you could set the id to be http://somesitehere.com/maliciouscode.php
and I'd also recommend the switch function
Hayden
one thing i thought of when doing include statements to to help further define WHERE that file is.

CODE
define("ABSPATH", $_SERVER['DOCUMENT_ROOT']);


then you can do

CODE
include(ABSPATH.$id.".php")


and somebody can't exploit your script to view files below the root.


add that with Indigo's post and you've got it made. tongue.gif
Ziggy
what would you do if you want to include a default 404 page?
Chris.
CODE
if(!file_exists(''.$id.'php')
{
     include("404.php");
}
else
{
     include(''.$id.'php');
}
Ziggy
thanks
nitr0x
The problem with using a switch function is that it's very time and space consuming. Plus when you add a new page, you have to add a new block of code again.
CB Productions
QUOTE (rus321 @ Jul 4 2006, 09:33 AM) *
CODE
switch($_GET['id']) {

case 'home':
include("index.php");
break;

default:
include("404.php");
}


You still need an IF to check if $_GET['id'] is set.
if($_GET['id']) {
$id = $_GET['id'];
switch($id) {

...

}
}

and switch isn't very useful if your site adds quite a lot of pages quite often.
Hayden
QUOTE (SpatialVisionary @ Jul 13 2006, 10:22 PM) *
one thing i thought of when doing include statements to to help further define WHERE that file is.

CODE
define("ABSPATH", $_SERVER['DOCUMENT_ROOT']);


then you can do

CODE
include(ABSPATH.$id.".php")


and somebody can't exploit your script to view files below the root.


add that with Indigo's post and you've got it made. tongue.gif

One minor change I would make to this, lol it's my own post.
CODE
define('ABSPATH', dirname(__FILE__));

Do that in your main index.php, btw.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.