Jump to content


Photo
* * * * * 1 votes

Simple and rapid navigation system


  • Please log in to reply
13 replies to this topic

#1 cigraphics

cigraphics

    Young Padawan

  • Members
  • Pip
  • 92 posts
  • Gender:Male
  • Location:London

Posted 01 June 2006 - 09:39 AM

I've made my own navigation system:
<?php
if($id == NULL) {
include('home.php');
} else {
include(''.$id.'.php');
} 
?>

let's break the code a little:
if($id == NULL)
If the $ID has no ?id=test or something it will include the home.php file
include('home.php');

else it will include the ?id=download and the file will be download.php
include(''.$id.'.php');

  • algorsassaunc likes this

#2 NinJA999

NinJA999

    Young Padawan

  • Members
  • Pip
  • 23 posts

Posted 22 June 2006 - 10:03 PM

First, you would have to do
$id=$_GET['id'];
to use the id variable.
Then, you could use this code to actually go to the pages rather than including them.
<?php
$id=$_GET['id'];
if($id == NULL) {
header("Location: home.php");
} else {
$theplace=$id+".php";
header("Location: $theplace");
}
?>


#3 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 23 June 2006 - 12:46 PM

Don't bother with using IF statements for navigation,

Simple use switch for something simple.

#4 rus321

rus321

    Young Padawan

  • Members
  • Pip
  • 49 posts

Posted 03 July 2006 - 06:33 PM

switch($_GET['id']) {



case 'home':

include("index.php");

break;



default:

include("404.php");

}

Edited by rus321, 03 July 2006 - 06:34 PM.


#5 cigraphics

cigraphics

    Young Padawan

  • Members
  • Pip
  • 92 posts
  • Gender:Male
  • Location:London

Posted 08 July 2006 - 12:53 PM

i know to do it with switch statement but i prefered to make my own

#6 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 11 July 2006 - 08:57 AM

Before including files, you could also check if the file exists:

<?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).

#7 Hit3k

Hit3k

    Young Padawan

  • Members
  • Pip
  • 120 posts
  • Gender:Male
  • Location:Australia

Posted 13 July 2006 - 05:55 AM

<?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....liciouscode.php
and I'd also recommend the switch function

#8 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 13 July 2006 - 10:22 PM

one thing i thought of when doing include statements to to help further define WHERE that file is.

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

then you can do

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. :P

#9 Ziggy

Ziggy

    Young Padawan

  • Members
  • Pip
  • 14 posts

Posted 12 April 2007 - 04:40 PM

what would you do if you want to include a default 404 page?

#10 Chris.

Chris.

    Young Padawan

  • Members
  • Pip
  • 129 posts
  • Gender:Male

Posted 12 April 2007 - 08:36 PM

if(!file_exists(''.$id.'php')

{

	 include("404.php");

}

else

{

	 include(''.$id.'php');

}


#11 Ziggy

Ziggy

    Young Padawan

  • Members
  • Pip
  • 14 posts

Posted 13 April 2007 - 04:18 PM

thanks

#12 nitr0x

nitr0x

    Young Padawan

  • Members
  • Pip
  • 201 posts

Posted 02 May 2007 - 11:35 AM

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.

#13 CB Productions

CB Productions

    Young Padawan

  • Members
  • Pip
  • 48 posts
  • Gender:Male
  • Location:Melbourne, Australia

Posted 26 June 2009 - 01:27 AM

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.

#14 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 15 September 2009 - 10:49 AM

one thing i thought of when doing include statements to to help further define WHERE that file is.

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

then you can do

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. :P

One minor change I would make to this, lol it's my own post.
define('ABSPATH', dirname(__FILE__));
Do that in your main index.php, btw.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users