Is this coding safe?

Discussion in 'PHP' started by chem, Oct 7, 2007.

  1. #1
    <?php
    if($_GET[page] == "") {
        include("main.php");
    } else{
    include($_GET['page'].".php");
    }
    ?>
    Code (markup):
    I have been told many times(by Godaddy once) that this include script is unsafe.

    Is it?
     
    chem, Oct 7, 2007 IP
  2. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you really have to do that, I would explicitly define the possible values. Like:

    if ($_GET['page'] == "members") { include ("membership.php"); }

    where the name of the page you're including isn't the actual name of the variable being passed. This will limit the possibilities that can be used. I can't think of any reason why you would absolutely need to do that though.

    What you're doing could potentially be a problem, because anyone could use a query string in the URL to view or pass information to ANY page on your site (including pages you might not want them to be sending information to), and could really do some damage if they just randomly start blasting your site with an automated script.
     
    james_r, Oct 7, 2007 IP
    chem likes this.
  3. chem

    chem Active Member

    Messages:
    288
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #3
    So confused =\

    Im trying to get it so the content part changes and not the side bars and such. This is the only way to do it that I know of.
     
    chem, Oct 7, 2007 IP
  4. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I would create a template file for the header, side bars, and footer and use them on each page, but create each page individually. that way, say you had 3 files:

    index.php
    contact.php
    aboutus.php

    all of those files would include header.php, sidebar.php and footer.php, but you don't have to pass any information about what the site is doing in the URL. To update your header, menu bar, or footer, you just have to edit the files once for the changes to happen across your whole site.
     
    james_r, Oct 7, 2007 IP
  5. chem

    chem Active Member

    Messages:
    288
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Sort of confused but I think I get what your saying

    Thank you!
     
    chem, Oct 7, 2007 IP
  6. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Or you can use it like this:

    
    <?php
    if($_GET['page'] == "") {
        include("main.php");
    } else{
        include(basename($_GET['page'].".php"));
    }
    ?>
    
    PHP:
    basename will strip every directory in the URL. Only problem with this method is that you cannot use values like "members/statistics" as a parameter anymore, because "members" would be stripped out as well.
     
    theOtherOne, Oct 7, 2007 IP
  7. roosevelt

    roosevelt Active Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #7
    if I were you, I would do this.
    
    <?php
    
    $page = strtolower($_GET['page']);
    
    if($page == "") {
        include("main.php");
    } else{
    include($page.".php");
    }
    
    ?>
    PHP:
    The only thing you will need to do is. In your URL enter the file name in upper case.

    For example http://www.yea-baby.com/index.php?page=ABOUTus

    This way a possible hacker might think the file name is ABOUTus.php, but in reality it is aboutus.php :).
     
    roosevelt, Oct 7, 2007 IP
  8. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #8
    But thing is, if you enter a URL such as http://www.yea-baby.com/index.php?page=../../some_private_file , you are able to reach a file which would normally be protected from being displayed.
    The strtolower-method will not work against that ;)
     
    theOtherOne, Oct 7, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    EDIT: At roosevelt.

    ^^ This isn't safe either. Use basename() and file_exists().
     
    nico_swd, Oct 7, 2007 IP
  10. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    even with basename they could still access any file in the same directory. I think this is a bad idea, I would avoid doing this altogether... Or track IPs & file access in a db so you know exactly who's accessing what files!!
     
    james_r, Oct 7, 2007 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    You can place the files you want to allow to include in a special directory.
     
    nico_swd, Oct 7, 2007 IP
  12. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Ahh yeah, I suppose there are ways to make it secure-ish. But it seems like an awful lot of work compared to just making a template file for the menu bar, header and footer and including them in all your pages.
     
    james_r, Oct 7, 2007 IP
  13. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #13
    file_exists and basename are the way to go. Anything else is just plain insecure.

    
    <?php
    $pagedir="/home/site/www/pages/";
    $page=basename($_GET['page'],".htm");
    // Just to be extra super paranoid:
    if (!eregi("^[a-z0-9\-]+$",$page)) {
        echo "Invalid character detected";
        exit;
    };
    $page=$page.".htm";
    if (file_exists($pagedir.$page)) {
        include $pagedir.$page
    } else {
        include "main.php";
    }
    ?>
    
    PHP:
    Depending on what you're doing this will give you a great way to load/maintain pages as only the content part will change. All the other bits can be kept in this one php file.
     
    tandac, Oct 7, 2007 IP
  14. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #14
    James_r,

    Lets assume the file above is called loader.php :)

    If we put all other template related items into loader.php you then have one place to manage and maintain the look of your entire site.

    Want to add a new menu item? Edit loader.php
    Want to restructure the look of the whole site? Edit loader.php

    While your way is good it means editing multiple PHP files for the same effect. At least in the case of change 2.

    If anyone can find a flaw in the above code I'd be interested to see it. :)
     
    tandac, Oct 7, 2007 IP
  15. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #15
    I have a question, why not create static shtml/php pages and use an include for the header and footer code?
     
    tarponkeith, Oct 7, 2007 IP
  16. theOtherOne

    theOtherOne Well-Known Member

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #16
    This would be the solution james_r suggested!
    But if you want to restructure the whole layout of the site lateron (like including another script at every call) it is harder to manage, because you would have to change every single page.

    In this solution (a central "loader" page, which includes other pages depending on how it is called) only one page needs to be changed, which makes it a little easier to handle ;)
     
    theOtherOne, Oct 7, 2007 IP
  17. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #17
    Most people would do:

    <?php include "header.php"; ?>
    PHP:
    ... html content ...
    <?php include "footer.php"; ?>
    PHP:
    There's nothing horribly wrong with this idea. I'm just suggesting a different way of thinking.

    Instead create a loader.php file with all the relevant bits you want:
    
    <?php
    include "header.php";
    include "navbar.php";
    // include page loader code from above
    incldue "footer.php";
    ?>
    
    PHP:
    All the pages in your site would then be referenced as:
    http://www.site.com/loader.php?page=contact

    For example. You would then have a file called conact.htm with just the appropriate contact us information. Since most sites follow a template structure this works quite well.

    Lets say for some strange reason you want to remove the navbar from all your pages. With your code you'd have to edit several PHP files and delete the include line. (Yes I know you could just empty out navbar.php). With the above code you would just delete the line from one file.
     
    tandac, Oct 7, 2007 IP
  18. tarponkeith

    tarponkeith Well-Known Member

    Messages:
    4,758
    Likes Received:
    279
    Best Answers:
    0
    Trophy Points:
    180
    #18
    Oops, didn't read all the replies...

    For me, this is my favorite option:

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    
    <title>Unique page title - site title</title>
    <meta name="description" content="unique description for each page" />
    
    <!--#include file="my_header.asp"-->
    
    <h1>content</h1>
    blah blah, main content
    <br /><br />
    
    <!--#include file="my_footer.asp"-->
    
    
    HTML:
    this way, each page has it's unique title, descriptions, keywords... if you want to edit the layout, just change the header and footer files... I'm not sure, but from what I heard, google likes static pages better then ? pages better too...
     
    tarponkeith, Oct 7, 2007 IP
  19. Declan127

    Declan127 Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Try this tutorial I wrote:
    namepros.com/webmaster-tutorials/293008-php-tutorial-index-php-page-yourpage.html
     
    Declan127, Oct 7, 2007 IP
  20. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Well, I went out of my way to write a script to show you why it's insecure, but I didn't think it through very well and ended up just writing a cURL based brute-force username/password cracker that would work on your solution or mine if the site had a login form. It's a pretty simple script that doesn't account for captchas or time-delayed IP banning for multiple login attempts (but that's easy enough to add in). I won't post it here... it's not exactly what I was going for. :(

    So in the end, I think it's reasonable to say that your solution is as secure as any other, but I have to agree with tarponkeith that's it's good form to use a template based approach (shtml or PHP) that allows you to use different title/keywords/meta tags on each page. But that could easily be added into your solution. Good stuff! :cool:
     
    james_r, Oct 7, 2007 IP