Looking for simple php navigation script - ?p=1, ?p=2 etc..

Discussion in 'PHP' started by xtreme fever, Feb 15, 2008.

  1. #1
    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
     
    xtreme fever, Feb 15, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    zerxer, Feb 15, 2008 IP
  3. xtreme fever

    xtreme fever Active Member

    Messages:
    472
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #3
    Thanks. I was hoping to find a script that will load all the html in page_2 and not content that is required.
     
    xtreme fever, Feb 15, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    zerxer, Feb 15, 2008 IP
  5. xtreme fever

    xtreme fever Active Member

    Messages:
    472
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    95
    #5
    Thanks. I'll see what I can do with it.
     
    xtreme fever, Feb 15, 2008 IP
  6. kineticdc

    kineticdc Peon

    Messages:
    347
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just what I was looking for, thanks
     
    kineticdc, Sep 1, 2008 IP
  7. BtSEO

    BtSEO Peon

    Messages:
    111
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I need to do this, thanks for the tips
     
    BtSEO, Sep 1, 2008 IP
  8. sanjay_bhuarya

    sanjay_bhuarya Peon

    Messages:
    379
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thx for the tip dude, i shall be adding it soon :)

    keep thm comin :)
     
    sanjay_bhuarya, Sep 19, 2008 IP
  9. classic

    classic Peon

    Messages:
    96
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    classic, Sep 19, 2008 IP
  10. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #10
    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:
     
    ads2help, Sep 21, 2008 IP
  11. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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.
     
    zerxer, Sep 21, 2008 IP
  12. pixeladd

    pixeladd Banned

    Messages:
    2,238
    Likes Received:
    93
    Best Answers:
    0
    Trophy Points:
    0
    #12
    just found this on searching google for something similar lol

    dunno why i didnt search DP first
     
    pixeladd, Sep 22, 2008 IP
  13. Sillysoft

    Sillysoft Active Member

    Messages:
    177
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #13
    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
     
    Sillysoft, Sep 22, 2008 IP
  14. gopalkr

    gopalkr Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #14
    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.
     
    gopalkr, Aug 31, 2009 IP