Include pages specified in the URL?

Discussion in 'PHP' started by cc2365, Nov 28, 2008.

  1. #1
    Now, I include body.php into main.php by putting the following code into index.php:

    <?php include ("body.php"); ?>
    PHP:
    My problem is, how to include pages specified in the URL? For example, when I visit:

    index.php?dir=products, it will include products.php, when I visit
    index.php?dir=db/core, it will include core.php, which is put in the folder "db".

    And if nothing is specified in the URL, it will include home.php. How can I do that? Helping will be appreciated!
     
    cc2365, Nov 28, 2008 IP
  2. diligenthost

    diligenthost Peon

    Messages:
    685
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This should work :)
    <?php
    if (isset($_GET['dir']) && $_GET['dir'] != "") {
    $dir = $_GET['dir'];
    if (file_exists(''.$dir.'.php')) {
    @include (''.$dir.'.php');
    } elseif (!file_exists(''.$dir.'.php')) {
    echo 'Page not found!';
    }
    } else {
    @include ('home.php');
    }
    ?>
    Code (markup):
    For the db/core thing to work, you'd probably have to assign another variable, something like "?dir=db&page=core". The script above will only check if products.php exists in the same folder, once a url like "?dir=products" is passed to it. You could maybe make a regex (regular expression) to find before and after the / in db/core with the folder & page name.
     
    diligenthost, Nov 28, 2008 IP
    cc2365 likes this.
  3. irunbackwards

    irunbackwards Peon

    Messages:
    791
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Or, an extremely simplified version of the above:

    if(isset($_GET['dir'])) {
    $dir = $_GET['dir'];
    include("$dir.php");
    }
    Code (markup):
     
    irunbackwards, Nov 28, 2008 IP
  4. diligenthost

    diligenthost Peon

    Messages:
    685
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This way is much more vulnerable to attacks, especially if allow_url_fopen is enabled in PHP ;)
     
    diligenthost, Nov 28, 2008 IP
  5. cc2365

    cc2365 Member

    Messages:
    91
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Thanks for helping!
     
    cc2365, Nov 29, 2008 IP