Publishing System Settings Logout Login Register
Easy Php Navigation using the ?id=pagename feature
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on February 21st, 2007
11638 views
PHP Coding
Hello,
In this tutorial you'll learn an easy way of navigation using the ?id=pagename feature.
BUT YOU MUST READ THIS IN ORDER TO UNDERSTAND IT AND THAT IT WORKS FOR YOU.

PS: Notice that this is my first Php tutorial so if you get a little confuse just reply back, but I think you'll do good. DONT FORGET TO VISIT MY SITE ScyberSpace


ok Lets start with the whole code. Make a file that you want to use (eg. index.php) if you use index.php the the url will be index. php?id=pagename you are not limited to the name of the file but use a logic name.
-index.php-
<?Php 
    // Define our array of allowed $_GET values
        $pass = array('tutorials','affiliates','contact_us');
           
    // If the page is allowed, include it:
        if (in_array($_GET['page'], $pass)) {
            include ($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['page'] . '.php');
        }
       
    // This will load the default page:
        elseif (!isset($_GET['page'])) {
            include ($_SERVER['DOCUMENT_ROOT'] . '/home.php');
        }

    // If the page is not allowed, send them to an error page:
        else {
                // This send the 404 header
                    header("HTTP/1.0 404 Not Found");
                // This includes the error page
                    include ($_SERVER['DOCUMENT_ROOT'] . '/error.php');
        }
?>

ok and now lets break it down to understand it
Code:$pass = array('tutorials','affiliates','contact_us');

This part of the code will array our pages in this cases it will array tutorials,affiliates,contact_us. To array your more of you own pages simply add the name of another page, so if you have a page name downloads.php you'll just add downloads as it is on the file.( be carefull when adding more pages if they have uppercase, just add it as it is)


        if (in_array($_GET['page'], $pass)) {
            include ($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['page'] . '.php');
        }

Here you'll determine if you want your page to have ?id, ?page or something else, just rename the $_GET['page'] to something else like $_GET['action'] , then your urls would be index.php?action=pagename. In ( '/' ) this part of the code you just establish where the pages are located if the are in the main folder leave it like it is but if is in something like news then name that the pages are in the news directory. Then in the last part again ask us for the $_GET value, you just name it with the same you did in the $_GET value above. And finally you add the extension of the pages you want to load. All pages must be the same extension in this case the all are .php.


        elseif (!isset($_GET['page'])) {
            include ($_SERVER['DOCUMENT_ROOT'] . '/home.php');
        }

This just loads the default page when you go to index.php. Make sure that the $_GET value is the same from above.


        else {
                // This send the 404 header
                    header("HTTP/1.0 404 Not Found");
                // This includes the error page
                    include ($_SERVER['DOCUMENT_ROOT'] . '/error.php');
        }

This final part of the code will just state that if any of the pages you put were not loaded then it will take you to a 4040 error an you just replace with your custom 404 error page at the end where it says ['DOCUMENT_ROOT'] . '/error.php');


I hope you've learn something from here and good luck
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
ScyberWolf

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top