May 18, 200719 yr Hey guys 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&page=latest.php" alt="">Latest News</a> //i use & because its more valid than & or something like that.
May 19, 200719 yr awesome tutorial mate, very in depth. If you want to leanr about .htaccess Ben, read this: http://www.pixel2life.com/tutorials/count/...ssive_tutorial/
May 19, 200719 yr Author 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
May 20, 200719 yr 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 Wildo
May 20, 200719 yr Author 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.
May 21, 200719 yr Author Well you already seem to know how to use .htaccess so you should have no problem.
March 28, 200818 yr 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.tutorialtastic.co.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.
Create an account or sign in to comment