Web Design Forum: TUTORIAL: PHP Navigation - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: PHP Navigation ?folder=blah&page=mneh Rate Topic: -----

#1 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 18 May 2007 - 10:36 PM

Hey guys :D Thought i'd make my php navigation into a small tut, as every website needs to contain a php navigation script, there are just so many.

Now, i know a lot of php navigations use switch. This means that the switch calls upon a case from a list of cases to switch between. This can save you a lot of files, as pages are kept lower. However, if you have a website that requires lots of pages, and you dont want to have to constantly update your navigation.php file or whatever, then take a look at this:

So, with all php, we need to start with our opening and closing tags:
<?php

?>


Right, now we need some variables in the code:
<?php

$folder = $_GET['folder'];
$page = $_GET['page'];

?>

This will use the $_GET method of receiving our folders and pages from the url.

Now we get to the if statements:
<?php

$folder = $_GET['folder'];
$page = $_GET['page'];

if (empty($page)) //if the page is empty
	 {  
		include('home.php');  //include your default page
	 }  
	 
else  //otherwise,
	
	 {  
	  
include('error_page.php');  //include an error page, if the page cannot be found.

	 };  
	 
};  

?>


Now this is great, but it either displays home.php or error_page.php, it wont actually show any of our pages, so

This will now use the variables to get the folder and page:
<?php

$folder = $_GET['folder'];
$page = $_GET['page'];

if (empty($page)) //if the page is empty
	 {  
		include('home.php');  //include your default page
	 }  
	 
else  //otherwise

	 {  
	  
	if (file_exists($folder . '/' . $page . '.php'))  //if the folder and page exists
	
	 {  
	  
	include($folder . '/' . $page . '.php');  //include that folder and page
	
	 }  
	 
	else //otherwise
	
	 {  
	  
include('error_page.php');  //include an error page, if the page cannot be found.

	 };  
	 
};  
?>


Now all you need to do, is make your links look like this:
<a href="http://www.yoursite.com/index.php?folder=news&amp;page=latest.php" alt="">Latest News</a> //i use &amp; because its more valid than &
or something like that.
0

#2 User is offline   BabyBen 

  • </cheeky>
  • PipPipPipPip
  • View gallery
  • Group: Members
  • Posts: 564
  • Joined: 13-March 07
  • Reputation: 0
  • Location:Scotland
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 19 May 2007 - 12:03 AM

You could tie this in with a .htaccess file, so you have domain.com/folder/page

:p
0

#3 User is offline   Cromo 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 422
  • Joined: 08-April 07
  • Reputation: 0
  • Gender:Male
  • Location:Leeds, UK
  • Experience:Beginner
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 12:37 AM

I need to load up my lynda.com Cd's and learn some php.
0

#4 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 07:34 AM

awesome tutorial mate, very in depth.

If you want to leanr about .htaccess Ben, read this:
http://www.pixel2life.com/tutorials/count/...ssive_tutorial/
0

#5 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 06:25 PM

Yeah, if you DO decide to use .htaccess with my navigation, make sure you change all your links to:
<a href="http://www.yoursite.com/news/latest (.php is optional)" alt="">Latest News</a> //This ties in with .htaccess, so the $_GET won't get all screwed up

0

#6 User is offline   RyanDaRin 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 55
  • Joined: 24-November 06
  • Reputation: 0
  • Location:Connecticut (USA)
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 19 May 2007 - 07:47 PM

Good tutorial :)

Also, how many people here are named Ben? 3+? lol
0

#7 User is offline   Cromo 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 422
  • Joined: 08-April 07
  • Reputation: 0
  • Gender:Male
  • Location:Leeds, UK
  • Experience:Beginner
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 08:05 PM

haha.

There is

Posted ImageBig Chief Ben

Posted ImageBaby Ben

and

Posted ImageThe Ben of the G.
0

#8 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 08:12 PM

View PostCromo, on May 19 2007, 09:05 PM, said:

haha.

There is

Posted ImageBig Chief Ben

Posted ImageBaby Ben

and

Posted ImageThe Ben of the G.

Ben G, da man :rolleyes:
0

#9 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 19 May 2007 - 11:26 PM

Stay on topic please ;)
0

#10 User is offline   Cromo 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 422
  • Joined: 08-April 07
  • Reputation: 0
  • Gender:Male
  • Location:Leeds, UK
  • Experience:Beginner
  • Area of Expertise:Designer/Coder

Posted 20 May 2007 - 12:01 AM

your only saying that because its your topic :p
0

#11 User is offline   Wildo 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 152
  • Joined: 16-May 07
  • Reputation: 0
  • Location:Holland
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 20 May 2007 - 07:19 AM

even though he has a point ;), I never user this kinda of navigation, I tend to keep as little files as possible, so most of the time I end up with a index file of at least 1500 lines of php or something :p

Wildo
0

#12 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 20 May 2007 - 08:09 AM

Thats the navigation i DID use. Depending on how my site is going to work, i might just use a switch navigation, and call functions from the function.php file.
0

#13 User is offline   BenG 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 766
  • Joined: 20-March 07
  • Reputation: 0
  • Location:Bradford, West Yorkshire
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 21 May 2007 - 06:26 AM

Nice.

Can iask, how does google handle URLS like thios?
0

#14 User is offline   Fault 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 35
  • Joined: 16-May 07
  • Reputation: 0
  • Location:UK [ Devon ]
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 21 May 2007 - 04:09 PM

Well you already seem to know how to use .htaccess so you should have no problem.
0

#15 User is offline   Mark 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 164
  • Joined: 15-October 07
  • Reputation: 0
  • Location:Southampton, Hants, UK
  • Experience:Intermediate
  • Area of Expertise:Designer

Posted 27 March 2008 - 03:12 PM

whats a php navigation..?
0

#16 User is offline   Daniel 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 128
  • Joined: 09-September 07
  • Reputation: 0
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 28 March 2008 - 01:49 PM

You should run some sort of validation on those two variables.
What happens if someone enters the URL manually and places a (..) as the folder variable?
Then can get access to different folders... so of which you may not want.

Here's another suggestion from a tutorial site: http://www.tutorialt...o.uk/snippets/1

Sorry if it feels like I just took your topic. Security comes first in my eyes or at least I try to work at it as best as I can.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users