WEB problem with php include

Discussion in 'OT Technology' started by drpepper, Mar 25, 2009.

  1. drpepper

    drpepper Active Member

    Joined:
    Nov 13, 2006
    Messages:
    38,076
    Likes Received:
    2
    Location:
    San Antonio
    So i have a site that i put all files that are being included in an 'includes' folder. i hate having a messy root folder, this is why i do this.

    here's my problem.

    index.php includes a sidebar.
    this sidebar needs to include some referrer script called log.php
    sidebar.php is in the includes folder.

    if i add it like this <?php include('./referrers/log.php'); ?> it works on index.php but it wont work on any pages where sidebar.php is being called but the parent file isnt on the root directory.

    public_html
    --index.php <-- works
    --includes
    ----sidebar.php
    --referrers
    ----log.php
    --mail
    ----error.php <-- does not work

    ('../referrers/log.php') does work in the error.php in the mail folder but not in index.php

    a solution would be to get rid of the includes folder and have header.php, footer.php and sidebar.php in the root folder, but id rather not if there's a simple solution to this.

    am i making any sense?
     
  2. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0
    Wirelessly posted via wap.offtopic.com (Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20)

    Best practice would be to use absolute paths.

    Ie /home/user/public_html/inc/file.php
     
  3. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,915
    Likes Received:
    8
    Location:
    Los Angeles
    I believe it won't work because sidebar.php may be trying to include the file relative to the path it's currently on. For example, if the other file you are trying to access (that's including sidebar.php) and it's in a random directory, it may try including "currentdir/referrers/log.php". I would suggest to do what hank says.
     
  4. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    I also agree with hank or at the very least using

    '/includes/sidebar.php'
    '/referrers/log.php'

    On most web servers '/' will path to public_html or www.
     

Share This Page