Help - Search - Members - Calendar
Full Version: quick/easy/secure php dynamic links
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
ShadowDeath01
well i have seen php navigation out there a lot of it, but all the ones i have seen give away your php page names here is one that dosen't if you name then right. here we go

CODE
<?php
$page = $_GET['id'];
switch($page){
case '': //sets the default page this is two single quotes not one double quote
include("index.php"); //defines the default page that will be shown
break; //break in the script so you can start a new one

case 'staff': //sets the name that will be shown in the address bar when you click the "Staff" link.
include("about.php"); //sets the staff page notice it is different than the case so people don't know your page
break; //breaks the script so you can start a new case
} // closes the switch
?> // closes the script


Lets break it down:

CODE
<?php

Opens the php code block

CODE
$page = $_GET['id'];

defines the $page variable and tells the script to get the page name after the = sign in index.php?id=whatever

CODE
switch($page){

tells the script to switch the name of the file such as about.php with the case set in "case"

CODE
case '':

defines the name of the page put two single quotes (not one double quote) and leave it blank in between to set the default page or put something in such as "staff" to make your link to the about.php page index.php?id=staff

CODE
include("index.php");

includes the page that the name will be switched with the "case" in this case it won't be switched because the case'': was left blank so this would be your default page

CODE
break;

puts a break in the script so you can define new "case"s and "include"s.

CODE
}

this closes the "switch" function

CODE
?>

this closes the php code block

This concludes my tutorial on quick/easy/secure php dynamic navigation. Hope this helps you in your future php coding!!!
Indigo
Instead of:
CODE
case '': //sets the default page this is two single quotes not one double quote
include("index.php"); //defines the default page that will be shown
break; //break in the script so you can start a new one


you could use:
CODE
default:
include("index.php"); //defines the default page that will be shown
break; //break in the script so you can start a new one


And when using these kind of links, your links should look like this:
CODE
<a href="?page=oneofthepagesintheswitch">Link to one of the pages</a>


Anyway, thanks for sharing, this might be useful for some smile.gif
ShadowDeath01
actually the links would be something like depending what page you put that in say it was index.php then the links would be
CODE
<a href="index.php?page=oneofthepagesintheswitch">Link</a>


guess i should have put that
Jem
I personally don't like dynamic includes - I try and reduce needed user input as much as possible. However, bias aside, the main problem with this snippet is that you need to add a new case & include() for each page you want to add. I recently fixed someone else's dynamic includes tutorial which prevents the need to update the php what-so-ever, and removes the risk of malicious usage/etc.
Copernicus
I find it fine using individual pages....like instead of member.php?do=register, just register.php.
Rory
Cool tut thanks. victory.gif
Arutha
QUOTE(Copernicus @ Sep 5 2006, 06:14 PM) *
I find it fine using individual pages....like instead of member.php?do=register, just register.php.


its all about your personal coding preferences smile.gif. I prefer to keep it in seperate files so i don't need to include every file to get all my functions.

Arutha
fosron
you can use
CODE
$page = $_SERVER['QUERY_STRING'];
instead of
CODE
$page = $_GET['id'];
, and the links will be
CODE
<a href="index.php?oneofthepagesintheswitch">Link</a>
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.