safer way to include a PHP file in a webpage?

Discussion in 'PHP' started by js09, Jan 26, 2010.

  1. #1
    in my index.php i'm calling all the content like so:
    
    <body>
    
    <?php include("header.php"); ?>
    
    <?php include("content_home.php"); ?>
    
    <?php include("footer.php"); ?>
    
    </body
    
    Code (markup):
    Is there a safer way to do this? Thanks.
     
    js09, Jan 26, 2010 IP
  2. wineri.es

    wineri.es Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What's wrong with your code?
     
    wineri.es, Jan 26, 2010 IP
  3. js09

    js09 Peon

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well it works just fine but I have been told by other coders that my method is not safe
     
    js09, Jan 26, 2010 IP
  4. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #4
    how about
    
    $file = file_get_contents("content_home.php");
    // Scan for malicious / unallowed code here
    
    
    // If nothing malicious is found run the code
    eval($file);
    
    PHP:
    But this example would just being useful if you let someone else add php files on the website.

    Not sure why they said it´s unsafe with include() though.. Might be if you don´t check for possible "Local File Inclusion" aka LFI you should google that. Except that there´s nothing wrong with include()
     
    n3r0x, Jan 26, 2010 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    Including static files is perfectly safe, just don't include files on a $_GET parameter.
     
    Kaizoku, Jan 26, 2010 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    There's nothing unsafe about this.

    It's unsafe to include($abc) where $abc came from a user. Or to include() a file on a remote server that you do not control. But including a specific known local file is fine.
     
    SmallPotatoes, Jan 26, 2010 IP
  7. radiotiger

    radiotiger Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Can anyone tell me how to include php code in tpl files. This normal method above gives error !
     
    radiotiger, Jan 29, 2010 IP
  8. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #8
    use the smarty variable {php} {/php}
     
    Bohra, Jan 29, 2010 IP
  9. crimprollc

    crimprollc Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I don't see any reason why that would be unsafe if they are static and your not attaching any variable to the end. You basically always want to scrub any user generated input. Static input is not usually a concern.
     
    crimprollc, Jan 29, 2010 IP
  10. leftnode

    leftnode Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yeah as long as content.php, header.php, etc are the same and not allowed to be changed, you should be fine.

    However, if they echo stuff that is inputted by the user, you need to sanitize it for XSS attacks.
     
    leftnode, Jan 30, 2010 IP
  11. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #11
    Just an observation: if you want to be strict with resource management, you should use include_once() and you don't need to open and close the PHP tags after each include:

    
    
    <html>
    ...
    
    <body>
    
    <?php 
    
    include_once("header.php");
    include_once("content_home.php");
    include_once("footer.php");
    
    ?>
    
    </body>
    
    ...
    </html>
    
    Code (markup):
     
    BRUm, Jan 30, 2010 IP
  12. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Bear in mind that include_once() is a lot slower than include().

    If you can ensure that it will only be called once, use include(). If you have multiple cross-dependencies, then you are probably stuck using include_once() or rolling your own mechanism which will likely be slower still.
     
    SmallPotatoes, Jan 30, 2010 IP
  13. gr8webseller

    gr8webseller Peon

    Messages:
    1,097
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #13
    it is safe and lots of peoples using this,,,,

    it also seems good
     
    gr8webseller, Jan 31, 2010 IP
  14. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #14
    That will produce huge overhead.
     
    Kaizoku, Jan 31, 2010 IP
  15. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #15
    That is impossible. People who are doing that are deluding themselves.

    There is always one more way to sneak some malicious code through. The only safe approach is never to execute un-trusted code in the first place.
     
    SmallPotatoes, Jan 31, 2010 IP
  16. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #16
    Including files which are hosted on your server/hosting space, then theirs no problem since you know you placed it their.

    But including remote files (which you don't have access to), could be potentially dangerous, so theirfore always clean the data before executing it.
     
    danx10, Jan 31, 2010 IP
  17. Rian

    Rian Well-Known Member

    Messages:
    1,763
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    125
    #17
    I do not see anything 'unsafe' about this. The code is good and will not produce any security risks.
     
    Rian, Jan 31, 2010 IP