Disadvantages of PHP includes?

Discussion in 'PHP' started by crazyryan, Jan 27, 2007.

  1. #1
    Hello

    I have been working on a site and I am using around 8 php includes so I don't have to edit all my pages manually if I decide to update, however my site sometimes goes extremely slow, but if I go to a page on the exact host and domain that doesn't have the includes it's fast.

    Could this be a result of the includes?

    PS. All my includes are like this:
    <?php include("http://www.domain.com/includes/navigation.php"); ?>
    <?php include("http://www.domain.com/includes/header.php"); ?>
    <?php include("http://www.domain.com/includes/footer.php"); ?>

    etc.
     
    crazyryan, Jan 27, 2007 IP
  2. dotboost

    dotboost Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    why you don't try to include files using relative paths, like:
    include('relative_path/navigation.php'); ?
     
    dotboost, Jan 27, 2007 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    what's a relative_path?
     
    crazyryan, Jan 27, 2007 IP
  4. dotboost

    dotboost Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    let's say you want to include in your main index.php the file navigation.php from the folder include.
    so you will have like this in your index.php:
    include ('include/navigation.php');
    That way you don't need to deal with absolute path like http://www.domain.com/includes/navigation.php
     
    dotboost, Jan 27, 2007 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    because if i have folders(which i do)
    the links become foldername/includes/navigation.php
     
    crazyryan, Jan 27, 2007 IP
  6. dotboost

    dotboost Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    dotboost, Jan 27, 2007 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    An absolute path doesn't have to be done via HTTP: an absolute path can be in your filesystem, just defined entirely from the root (if the filename starts with a forward slash, it's absolute).

    Including files via HTTP does not work as you would expect. Simple as that. It will give you the output of the parsed file, as opposed to the file itself.
     
    TwistMyArm, Jan 27, 2007 IP
  8. PureLuckk

    PureLuckk Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I guess this goes without saying, though he wasn't sure what one was so I think I may mention this: using relative paths is a lot faster than using absolute because (like TwistMyArm said) it does not have to connect to HTTP.
     
    PureLuckk, Jan 27, 2007 IP
  9. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I'm not sure that is true and I don't really think that is what TwistMyArm said.

    Depending on how deep your include_path is a relative pathname may be slower than an absolute pathname. An absolute pathname only needs to start looking from one point in the filesystem while a relative path may have to look in multiple places before the file is found. Whether relative or absolute there is no reason why it needs to connect via HTTP.

    There seems to be some confusion between the use of URLs in html and paths/files in php. In html the filename being used by crazyryan would be absolute. In PHP it is a URL - it is not a pathname. Indeed it can only be handled at all because allow_url_fopen and allow_url_include are set to true.
     
    streety, Jan 27, 2007 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    streety is correct with his understanding of what I was saying.

    Calling a file by a URL is not really referred to as calling it via an absolute path or a relative path. Absolute and relative refer to where a file is found in the filesystem with regards to the current position in the filesystem. Technically, calling a file via HTTP / URL is an absolute path but for all intents and purposes it isn't: it's calling a file from a remote machine (even though the 'remote' machine is the same one that the original script is running from).

    It's slower calling via HTTP because PHP makes a proper connection to the web server and calls the file that way. Like I was saying, too, the 'file' that gets included is not the source of the file, but the output of the file. It will include exactly the same stuff that you would see in your browser if you called the file that way. Hopefully you can see that that is a real security issue if your files are designed to return code that way.

    Sorry if I'm not making much sense... I have had very little sleep over the last few days.

    NEVER, NEVER, NEVER include or require a file via a URL UNLESS you are actually trying to import an already parsed file from another server. It may work, but it will NOT work as you think it will (unless, of course, you are thinking about it correctly :) ).

    Now, restrict your thinking just to the local filesystem. The difference between an absolute path and a relative path is quite simple: absolute paths give the full path to a file while a relative path gives the path to the file RELATIVE to the current one.

    Really bad analogy time.... Imagine you and I live in the same suburb: if someone asks me how to get to your place, I could give your full address. That would be an absolute path. Alternatively, I could say 'yeah, go down the street, take the second right then the first left. His is the second blue house on the right'. That would be a relative path. It's relative to whereever we are at the time.... those directions would not be correct anywhere else (whereas your complete address would be).

    Now, PHP has some problems with includes and relative paths: it all comes down to how the files are processed and merged, for want of a better word. In cases like this, instead of restating my points I always point to a thread on the Joomla forums where I made this point... so I'll point you to it, too :)

    http://forum.joomla.org/index.php/topic,49344

    I'm Narcissus on that forum and if you read through that thread you can see not only a (possibly) better explanation about why including via HTTP doesn't work properly, but also what problems can arise from using relative paths and how to overcome them while still using 'relative-like' paths.

    Feel free to ask more questions here, though!

    Ugh. Just re-reading that thread I noticed that I don't mention the 'relative-like' paths... I'll see if I find time to do up some text on the best way to go about using them.
     
    TwistMyArm, Jan 27, 2007 IP
  11. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #11
    why not use
    
    include_once(getenv('DOCUMENT_ROOT')."includes/navigation.php");
    
    Code (markup):
    You just have to worry in relation to root where the files are situated ?
     
    lfhost, Jan 27, 2007 IP
  12. toby

    toby Notable Member

    Messages:
    6,923
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    285
    #12
    yep, you can use require_once or include_once instead of inlcude.
    by the way, try not to have so much include. (8 is quite a lot, i would say).

    I also use FLUSH function to help boost the speed.
     
    toby, Jan 27, 2007 IP
  13. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #13
    lfhost: I don't mind that approach, it does make sense, however, I use a combination of dirname( __FILE__ ) and relative paths. Essentially, I prepend the relative path with the dirname( __FILE__ ) call which works as it should.

    That way when I look at the files, I can still see the 'relative' path but in reality it is converted to an absolute path.

    In my experience, 8 includes is not many at all. On top of that, the extra processing time required to include a file is miniscule (a file read operation is not noticable). In reality, the 'pros' of multiple include files far outweigh the 'cons' (eg. code redundancy).
     
    TwistMyArm, Jan 28, 2007 IP
  14. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #14
    I agree with TwistMyArm, 8 includes is not a lot. Consider all of the template systems such as Smarty etc which use a lot of includes, or PHPMyAdmin which has god knows how many.
     
    SilkySmooth, Jan 28, 2007 IP