Help - Search - Members - Calendar
Full Version: Simple PHP Navigation
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Squid
I've written a tutorial, for two purposes. First purpose, so people can learn how to make a site navigation with php, if they don't know yet. Secondly, so people can tell me how I can improve, or give me some piece of advice. Here's the tut:

Description: In this tutorial we'll learn how to make our site navigation like index.php?page=contact.
Skill: Beginner
Time needed: 15 minutes

As stated before, some experience with html (and php) is required, since we won't be going into too much detail. You can always at any time
ask questions on our forums, send us an e-mail or use the contact form.

Right, we'll start of with the main code:
CODE
<?
switch($_GET['id']) {

default:
include('home.php');
break;

case "news":
include('news.html');
break;

case "contact":
include('contact/contact.asp');
break;

}  
?>

You should paste this code into your main index file, on the part where you want your content to be displayed. The next thing you should do is edit the id's and the links. On top of the code it says the following:
CODE
default:
include('home.php');
break;
[/code
This is the default page that should be displayed. All the next bits that follow, are the id's with their associated links:
[code]
case "news":
include('news.html');
break;

As you can see, the id 'news' is linked to 'news.html'. You can name any id like you want to call it, and also any link. news.html, news.php, it
all works. Also, files which are located in sub folders can be linked. Watch the 'contact.asp' link. It's located in a folder called 'contact'. Also
this link will work. The id can be changed to whatever you like:
CODE
<?
switch($_GET['id']) {

The next thing you would do, is fix all your links in your menu's:

Add the following:
CODE
<a href="index.php?id=home">Home<a/>

Remember, once you change the 'id' to something else, like 'page', then you will have to update your links aswell:
CODE
<a href="index.php?page=home">Home<a/>

If you have for example a downloads page, you can add the main code posted above to a file called 'downloads.php'. Change 'id' to 'downloadid'
for example, and you can link like this:
CODE
<a href="index.php?page=home&downloadid=whatever">Home<a/>

Just play around with it a bit, and I'm sure it will work out for you.
Firebird
Thats pretty much the same code I use on my site.
Squid
Yeah I'm sure many use it, since in my eyes it's one of the easiest codes to use.
Chaos_Blader
just make sure you dont have any secret files cause someone might use

?id=../../secrets/ftppasswords.ext
Squid
QUOTE(Chaos_Blader @ Nov 6 2005, 09:01 PM)
just make sure you dont have any secret files cause someone might use

?id=../../secrets/ftppasswords.ext

Well that's not going to work, because you need to specify all the ?id= links into the code, so it wouldn't work.
liveman
why don't you do it like

CODE
<?PHP
if (empty($id) || !file_exists('/' . $id . '.php')
{
 include 'news.php'; //change to w.e
}
else
{
include '/' . $id . '.php';
}
?>

Thats what I use on my website ... a whole lot easier, and every time you add a page you do not need to add a case statement
Squid
Yup that's a better code, something I wasn't aware of yet. I'm using it myself now, thank you.

There's a lot of more ways to make such a php navigation. I just made a tutorial about the one I am/was using. Plus in my tutorial you'll learn about includes and the GET function. Slightly more advanced I believe. But the purpose of this tutorial mainly was to see how my tutorial writing skills are. Thanks again anyway bigwink.gif
Dark
Livemans code but shorter
CODE
<?php
$id = $_GET['id'];
(!$id || !file_exists("$id.php")) ? include 'news.php' : include "$id.php";  
?>


A quick explanation on what livemans code does to those who dont know:
Basically it checks if the variable id is empty or if a php file exists with that id, if either condition is true it includes news.php else it includes the requested file

I would recommend the case method as its securer
Ckristian
I am working on exactly this sort of thing. So the tut helped me out a bit bigwink.gif Thanks. smile.gif
Squid
QUOTE(Ckristian @ Nov 19 2005, 05:41 PM) *
I am working on exactly this sort of thing. So the tut helped me out a bit bigwink.gif Thanks. smile.gif


No problem, I'm glad it helped
Nike
I fail to see what is so good about using this?
Rob_BCFC
I dont understand how you get the extra bits like index.php?id=contact&contactmethod=email.

Any help?
Squid
QUOTE(Nike @ Dec 25 2005, 07:28 PM) *
I fail to see what is so good about using this?


There's nothing good about using it. It's just a tutorial, meant to help out people using some PHP variables. It's not a script I made, it's a tutorial, which is designed in the first place to help out people. Anyways, that's all I have to say about it.

QUOTE(Rob_BCFC @ Jan 3 2006, 10:45 PM) *
I dont understand how you get the extra bits like index.php?id=contact&contactmethod=email.

Any help?


Rob_BCFC. It's not the best method, but if you want such links, you should do the following:

On your index file youçe placed the code script. You've defined the id contact. Now, on your contact.php file, you'll also have to define the code. But this time instead of using id, you'll be using contactmethod. Since you work with cases, you'll have to pre-define everything first.

Example:

You index.php file is looking like this:

CODE
<?
switch($_GET['id']) {
default:
include('whatever.php');
break;
case "contact":
include('contact.php');
break;
}  
?>


And your contact.php should then look like the following:

CODE
<?
switch($_GET['contactmethod') {
default:
include('whatever.php');
break;
case "email":
include('email.php');
break;
}  
?>


I hope this answers your question.
rus321
ye this is a great tut tongue.gif

but isent mod_rewrite better:
its neater and good for SEO
Maximus-x
I used this scipt before. But now i found one problem. When trying to include a file that resides in another folder, say:

case "site9": include('webdesign/site9.htm',);

the page falis to show the jpgs. The jpgs reside in the folder webdesign. So does the htm. It´s weird. Can´t figure it out.

When i used it for the first time, all the php and htms lived in the root folder, and there was no problem with those.
But now i´m using these sub-folders...Any help will be greatly appreciated.

Maximus-x sad.gif
Jynxis
you need to specify where all the images are


so if an image is in "contact/images/email.gif"

and you include the page "contact/contact.php"

in the contact.php you will need to specify that the images are located in

"contact/images/*.*"

ie <img src="contact/images/email.gif">



probably confused you... but eh.


my way to do this is much eaier.

and uses less files. like 4 total.
auth.inc.php // for login
functions.php // functions such as "pages"
admin.php // admin control panel
index.php // main file.
ICT Helpers
I sure am liking the tutorials here. Great advice, thanks smile.gif
fedekiller
lol i prefer

<?php
if(isset($_GET['fd']) && isset($_GET['page']) && file_exists($_GET['fd']."/".$_GET['page'].".php")){
include ($_GET['fd']."/".$_GET['page'].".php");
}else if(isset($_GET['page']) && file_exists($_GET['page'].".php")){
include ($_GET['page'].".php");
}else if (empty($_GET['page'])){
include("main.php");//main page
}else{
echo "Error, that page does not exist.
";
}
?>

basycally make a folder called test and a file called testing.php
make yoursite.com?fd=test&page=testing
if it appear it worked tongue.gif
and you dont have to add case...blablabla each time u wanna ad a page
ICT Helpers
fedekiller, what is the advantage of your code and this post above? To me they do the same thing except yours also checks for a folder. Am I right with that understanding?
fedekiller
yes and the other big difference
each time you wanna add a page you ahve to do case(page):include.......
in my code you dont have to make it, just add a new page and call it ad ?page=newpage tongue.gif
ICT Helpers
You don't need to add a case statement in the post I linked to bigwink.gif

Thanks smile.gif
fedekiller
NP ^-^
Koncept
I would just go like this victory.gif
CODE
if(isset($_GET['id])) {
    $requestlct = $_GET['id'];
} else {
    $requestlct = 'idx';
}
$requestlct = str_replace("+", "/", $requestlct);
if (file_exists("source/$requestlct.php")) {
    include("source/$requestlct.php");
} else {
    include('source/errors/404.php');
}
ICT Helpers
QUOTE(fedekiller @ May 3 2006, 01:10 AM) *
NP ^-^
Not sure what you are saying there unsure.gif
Do you know what advantages one has over the other, or does anybody else know?
QUOTE(Koncept @ May 3 2006, 11:32 PM) *
I would just go like this victory.gif
CODE
if(isset($_GET['id])) {
    $requestlct = $_GET['id'];
} else {
    $requestlct = 'idx';
}
$requestlct = str_replace("+", "/", $requestlct);
if (file_exists("source/$requestlct.php")) {
    include("source/$requestlct.php");
} else {
    include('source/errors/404.php');
}
What are the advantages of using this code compared to the others posted? unsure.gif
mbx5nitro
In this code
CODE
if(isset($_GET['id])) {
    $requestlct = $_GET['id'];
} else {
    $requestlct = 'idx';
}
$requestlct = str_replace("+", "/", $requestlct);
if (file_exists("source/$requestlct.php")) {
    include("source/$requestlct.php");
} else {
    include('source/errors/404.php');
}


This part
CODE
if(isset($_GET['id])) {
    $requestlct = $_GET['id'];
} else {
    $requestlct = 'idx';
}

Calls the id the first part of that $requestlct = $_GET['id']; gets the file name that you put in the link. The else statment
} else {$requestlct = 'idx'; That is the default file it will get if nothing is specified. To make the idx work you must have a file in the source folder with the name idx.php so the path to your idx file would be "source/idx.php"

This part
CODE
$requestlct = str_replace("+", "/", $requestlct);
if (file_exists("source/$requestlct.php")) {
    include("source/$requestlct.php");
} else {
    include('source/errors/404.php');

Checks to see if the file exist and if it does it sends you to "source/$requestlct.php" where $requestlct.php is the page name. like if your link was index.php?id=news the $requestlct.php would turn into news.php. If the file you specifed doesnt exist you are sent to the 404.php page which is your error page.

Hope that helps. Thats a great way to do the navigation although ALL your files must have the .php extention to work.
ICT Helpers
Thanks for the explanation mbx5nitro victory.gif My query however is that code better than the code that was posted earlier in the topic?

QUOTE(Dark @ Nov 19 2005, 02:40 PM) *
Livemans code but shorter
CODE
<?php
$id = $_GET['id'];
(!$id || !file_exists("$id.php")) ? include 'news.php' : include "$id.php";  
?>


A quick explanation on what livemans code does to those who dont know:
Basically it checks if the variable id is empty or if a php file exists with that id, if either condition is true it includes news.php else it includes the requested file

I would recommend the case method as its securer
mbx5nitro
Im not sure i dont understand the security aspect enough to say i would say the code i explained is better but i dont know. maybe someone else can say more.
ChaMChoM
man that tut helped me alote thanks
Hayden
QUOTE(Chaos_Blader @ Nov 6 2005, 09:00 PM) *
just make sure you dont have any secret files cause someone might use

?id=../../secrets/ftppasswords.ext


You would be correct, assuming he was doing something like include $_GET['id'].".php"; tongue.gif

which i think is bad coding all together without some sort of checking of the user input. I think I've read it hear before as well as elsewhere, but as you code, you should never trust user input. Even by accident, someone can find a vulnerability.
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.