Hello, I am looking for a simple php navigation script so I can link using index.php?p=page_1, index.php?p=page_2, etc... I found a script but it does not work the way I hoped it would. Thanks
This is fairly simple. First, you would make all your links like you just specified (index.php?p=page_1, etc). Then, in the content area of your site, where the data changes based on what page they're on, you can put the following (a very simple method for it): <?php $page = $_GET['page']; $file = "pages/".$page.".php"; if(file_exists($file)) { include($file); } else { print "404 Error. Page does not exist"; } ?> PHP: Based on this script, you'd then create a "pages" directory in the same directory as your index. You'd put your content files in there such as page_1.php, etc. In your page files, you would only put the content that is required for that page, not the rest of the template HTML. It does not have to have PHP in the file, either, it can be all HTML or plain text if you want. This is just a very simple, working example that you can use.
Thanks. I was hoping to find a script that will load all the html in page_2 and not content that is required.
My method will still load all the content that's in the page file, I just meant if you put that PHP code in the center content area that you do not have to recreate all the template HTML since it's already in the index itself. You can do whatever you wish with it.
Do be carefull when doing this kind of coding as somebody could access some other files on a server eg index.php?p=page_1 to index.php?p=../admin/index
just a dumb idea for consideration lol make an array of files available/allowed to include, eg: $allowed_page = array( 'page1', 'page2' ); PHP: and disallow any other $_GET['p'] value that is not in the array. if (in_array( $_GET['p'], $allowed_page)) { include ($_GET['p']); } else { // the $_GET['p'] value is not allowed to include() // what u want to do } PHP:
function IsSafeInclude($x) { if(strpos($x, "/../") !== false || strpos($x, "../") === 0 || strpos($x, "/..") == (strlen($x) - 3) || $x == '..') return false; else return true; } PHP: Quick function I just typed up to address the issue of someone trying to crawl through your directories if you use the method I posted earlier.
I agree make sure you have checks for this. An array or a switch will confine the script to only load pages that should be loaded instead of allowing some security hole in your script. Silly
The URLs with ? mark in them are not very friendly with Google analytics tool. Search engines wise as well they are not so good though it does not matter much. Google analytics does not print the full URL when this type of URL is encountered.