SEARCH
Newsletter
Subscribe to get design tips, latest trends, free stuff and more.
It doesn't look like an e-mail address

hosting

  Web Programming PHP Navigation Using Switch

Navigation Using Switch

Will PHP Mar 30, 2007

Navigation Using SwitchSo these days there are ways to exploit your PHP automated navigation with selecting files from modules and including them autimatically according to the URL variables. Not good.

Today I'm going to explain to you a simple method, which is safe and easily customizeable.

<?php
$x = $_GET[x];
switch($x) {
default:
include('../home.php');

This is just the beginning of our navigation script. I'll explain this for you...

The first line gets $x from the URL. We don't have to worry about htmlspecialchars or stripslashes, because there are a finite amount of options to choose from later on.

switch($x) starts an "array" of options that $x must comply with.

The 3rd and 4th lines are what the script will do if $x is not set, or does not comply with the rules set.

Moving onto the next chunk...

break; case "tutorials":
include('../tutorials.php');

This code basically translates to:

if($x=="tutorials") {
include('../tutorials.php');
}

Which translates to English as: "If $x's value is tutorials, include tutorials.php"

To access tutorials.php, you'd go to www.site.com/switchfile.php?x=tutorials

So we'd go on and on for a while, eventually ending up with something like this:

<?php
switch($x) {
default:
include('../home.php');

break; case "tutorials":
include('../tutorials.php');

break; case "snippets":
include('../snippets.php');

break; case "script_downloads":
include('../script_downloads.php');

break; case "banners":
include('../banners.php');

break; case "code_lessons":
include('../code.php');
}
?>

What you do now is quite similar to what you do with any other navigation. Put this script in whichever page you want, where you want the content to be, and then upload it. You're sorted.

Just to recap, say you put this script on www.site.com/nav.php, you'd go to www.site.com/nav.php?x=banners to include the banners page.

subscribe to newsletter