Include file varying on URL

Discussion in 'PHP' started by peter_anderson, Jan 25, 2009.

  1. #1
    Hi

    I am trying to include a file which varies on which URL the user visits.

    Eg - if they visit articles.php?a=1 it will give them /articles/1.txt included, and for articles.php?a=hi, it will include /articles/hi.txt

    This is the code I am using, but it does not include the file, even though it exists.

    <?php
    $sArticleName = isset($_GET['a']) ? $_GET['a'] : 0 ;
    $sArticleFile = sprintf('%sarticles/%s.txt',
        $_SERVER['DOCUMENT_ROOT'],
        $sArticleName
    );
    include('header.php');
    if(file_exists($sArticleFile) && is_readable($sArticleFile))
    {
        include($sArticleFile);
    }
    else
    {
        echo '<p>No articles found matching your criteria.</p>';
    }
    include('footer.php');
    ?>
    
    Code (php):
    Anyone any ideas what is wrong with it?
     
    peter_anderson, Jan 25, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    crivion, Jan 25, 2009 IP
  3. peter_anderson

    peter_anderson Notable Member

    Messages:
    3,382
    Likes Received:
    152
    Best Answers:
    0
    Trophy Points:
    240
    #3
    Thanks, but that's not what I mean.

    What I want is that the all I need to do is upload a txt file to the articles directory, and link to it. The script aint for me, and the person it is for doesnt want to have to edit a php file for every new page.
     
    peter_anderson, Jan 25, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Looks fairly decent,

    var_dump($sArticleFile) for me and tell me the result, is this the correct folder where the articles are stored?
    Does using alternative means (i.e. file_get_contents) load the file correclty?

    Dan.
     
    Danltn, Jan 25, 2009 IP
  5. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    for security id use an array for allowed tags like so

    
    
    $allowed_includes = array("file1" => "file1.php","file2" => "file2.php");
    
    if(isset($_GET['a'])){
         if(in_array($_GET[a],$allowed_files)){
             @include $allowed_types[$_GET['a']];
         }else{
                die('HACKING ATTEMPT!');
         }
    }
    
    PHP:
    thats just a quick referance ... when including files by url make sure you look into the security well!
     
    cornetofreak, Jan 25, 2009 IP