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.

PHP Include and Different Folders

Discussion in 'PHP' started by Daniel591992, Oct 21, 2008.

  1. #1
    Alright, so here's the deal:

    I'm working on a basic site using PHP Includes so I don't have to update each page when making design changes. Well, It was all working fine until I decided to use various folders. For example, what used to be tools.php is now /tools/index.php. But, when using PHP include in the new /tools/index.php, I get problems since it looks for the include files (header.php, head.php, and footer.php) in /tools/.

    I tried using this:
    <?php include($DOCUMENT_ROOT . 'head.php'); ?>

    But it didn't work. Then I tried this:
    <?php include($DOCUMENT_ROOT . '/home/daniel59/public_html/head.php'); ?>

    Is there anyway of defining Document Root? But, anyways, I get no error messages. The problem is that head.php links to style.css, but, when I view the source for /tools/index.php, it looks for style.css in /tools/style.css. The file doesn't exist, of course, since style.css is located in /home/daniel59/public_html/style.css

    Ahhh, i'm confused
     
    Daniel591992, Oct 21, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    echo $_SERVER['DOCUMENT_ROOT'];
     
    bartolay13, Oct 21, 2008 IP
    Daniel591992 likes this.
  3. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    that's right. The problem is that you have relied on register globals, which are turned off in your enviroment (which is a good things)

    to find out why it wasn't working, just google register globals
     
    Kyosys, Oct 22, 2008 IP
    Daniel591992 likes this.
  4. Daniel591992

    Daniel591992 Well-Known Member

    Messages:
    594
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    125
    #4
    Thanks. That worked for setting the home directory, but how do I fix the CSS problem?

    My CSS file (/style.css) has lines like:

    html {
    background: url(/img/bg.jpg) repeat;
    }

    The CSS file won't load since it is in head, and head is set to look for it at:

    <link rel="stylesheet" href="style.css" type="text/css" />

    So, /tools/index.php is looking for the file tools/style.css, which doesn't exist.

    How can i fix that?
     
    Daniel591992, Oct 22, 2008 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    if its <link> try to follow your folder organization based on where it is <link>

    just use ../img/bg.jpg if going up one level
    and w/o ../ if under this folder...

    anyways its css just try to follow your folder organization.
     
    bartolay13, Oct 22, 2008 IP
  6. Daniel591992

    Daniel591992 Well-Known Member

    Messages:
    594
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    125
    #6
    That works, but is there a better way of doing this? Like, a way of getting head.php, header.php, and footer.php to automatically reset the paths to the root?


    EDIT: Just found out what I had to do. I had to put a slash in the paths. So, for example, in head.php, I had to change this:

    <link rel="stylesheet" href="style.css" type="text/css" />

    to this:

    <link rel="stylesheet" href="/style.css" type="text/css" />

    and it works. Thanks to everyone
     
    Daniel591992, Oct 22, 2008 IP
  7. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #7
    you mean dynamic css? just one css file but is still compatible with other pages..
    just put your css code into a php file
    with a <style></style>
    just insert a php variable inside it

    ie.
    #container .folder {
    background: url(<?=$_SERVER['DOCUMENT_ROOT']...?>/folder/image.jpg);
    }
     
    bartolay13, Oct 22, 2008 IP
  8. Daniel591992

    Daniel591992 Well-Known Member

    Messages:
    594
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    125
    #8
    You might have missed my edit. Take a look above, I figured it out. Thanks!
     
    Daniel591992, Oct 22, 2008 IP