1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

INCLUDE inside of an IF STATEMENT

Discussion in 'PHP' started by mma freeroll, Jan 30, 2009.

  1. #1
    Does anyone know if code like this accessess b.php or c.php

    
    $test = "a";
    if ($test == "a") include("a.php");
    if ($test == "b") include("b.php");
    if ($test == "c") include("c.php");
    
    PHP:
    I'm wondering because I have a ton of BIG includes that I may only need to access if a certain variable is present.

    I know that even though they won't execute (because of the if statement), PHP may still grab the contents, right? And that takes server power, right?
     
    mma freeroll, Jan 30, 2009 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It won't execute or grab the contents if the IF statement is false.
     
    GreatMetro, Jan 30, 2009 IP
  3. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    If the variable determines which file to include you could do this:

    include($test.".php");
    PHP:
    Even if it doesn't, you can still do this:

    
    $filename="";
    switch($test)
        {case "xyz":   $filename="somefile";    break;
        case "abc":    $filename="another";     break;
        case "jsc":    $filename="blahblah";     break;
        ...
        ...
        ..
        case "xxx":   $filename="lastfile";    break;
        }
    include($filename.".php");
    
    
    PHP:
     
    plog, Jan 30, 2009 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, if you wanted to feel safer, you could just do one include statement and choose the appropriate name like plog suggested. But if an IF statement is not executed then none of the code in it is executed so include() is never called and the contents are never pulled and no server power is used prefetching all the includes.
     
    zerxer, Jan 31, 2009 IP
  5. bbrez1

    bbrez1 Banned

    Messages:
    208
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is the easiest and most secure way:

    
    
    switch($_GET["page"])
    {
        case "contact":
         include 'contact.php';
        break;
    
        case "info":
         include 'info.php';
        break;
    
        case "products":
         include 'products.php';
        break;
    
        default:
         include 'default.php';
    }
    
    Code (markup):
    Lets say for the examples u guys mentioned, what if someone enters "lalalal" in the url via get method? This way the default part in switch will assure that the default (index) page will be shown.
     
    bbrez1, Jan 31, 2009 IP
    gemini181 likes this.
  6. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Or...
    include("./pages/" . (file_exists("./pages/" . $_GET['page'] . ".php") ? $_GET['page'] : "default") . ".php");
    PHP:
    One line. Yes, I realize that is a big security risk, though. They could crawl your directories like that. So here's a function to fix it:

    function IsSafeInclude($x) {
        if(strpos($x, "/../") !== false || strpos($x, "../") === 0 || strpos($x, "/..") == (strlen($x) - 3) || $x == '..')
            return false;
        else
            return true;
    }
    include("./pages/" . (file_exists("./pages/" . $_GET['page'] . ".php") && IsSafeInclude($_GET['page']) ? $_GET['page'] : "default") . ".php");
    
    PHP:
    If you're using a method like page inclusion based on what's in the URL, I'd recommend storing the pages in a separate folder like "pages" like I assumed in the code above.

    Nothing wrong with your Switch statement, it's secure enough, I just don't like having to add a line or so to it every time I want to add a new page.
     
    zerxer, Jan 31, 2009 IP
    tarponkeith likes this.