Forum Moderators: coopster

Message Too Old, No Replies

include problem

         

andrewsmd

7:04 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am helping a client include a header page in all of his files. I ran into a weird problem. The directory structure is something like this
root/master/header.php
root/file1.php
root/folder1/file2.php
root/folder1/anotherFolder/file3.php
root/css/style1.css

In header.php I have the tag
<link href="../css/style1.css" rel="stylesheet" type="text/css" />

in file1.php I have the line
require_once("master/header.php"); //everything works fine

in file2.php I have the line
require_once("../master/header.php"); //everything works fine

in file3.php I have the line
require_once("../../master/header.php");

The problem I find is that the css stylesheets don't get loaded. The header page loads, but none of the styles are there. I think it has something to do with everything being in different folders, and that I am going up two directories but I have no idea how to fix this. Can anyone help? Thanks,

rainborick

8:28 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your header.php file is generating the HTML code for the <link> tag that browsers will see only as a part of the main document, so your addressing for the outputted HTML needs to be based on the location of the main document, not the included PHP file.

andrewsmd

8:38 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I kind of understand what you are saying, do you have a general suggestion about how to do that? Thanks,

smb2009

10:10 pm on Sep 23, 2009 (gmt 0)

10+ Year Member



If you're generating your file to output in the same directory as the file3.php i.e.
root/folder1/anotherFolder/file3.php

your header.php file is looking for the css file relative to that location i.e.

root/folder1/css/style1.css and not in

root/css/style1.css

This thread may help you resolve this
[webmasterworld.com...]

Hope that helps.

Dinkar

1:39 am on Sep 24, 2009 (gmt 0)

10+ Year Member



In simple words, use

<link href="http://www.yoursite.com/css/style1.css" rel="stylesheet" type="text/css" />

andrewsmd

1:33 pm on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Feel like an idiot. Thanks,

rocknbil

6:55 pm on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, don't feel like an idiot, it's a common problem - and don't use the full url*. People often confuse the difference between a "client side" file inclusion and a "server side" file inclusion. They are not the same.

Use a starting slash for client side, full server path for server side.

In client side - in the browser pages, whatever you can view source and see - this always means "start at the domain root and look along this path"

/path/to/css/style.css

CSS looks for paths [as in, url(/images/image.jpg)] relative to the css file itself, external Javascript relative to the document including it - in either case you can move that css file seven directories deep, and the above will always work so you don't have to futz around with the ../../../dot-toothpick syndrome.

For PHP includes, it's not a URL, it's a system path. So for larger systems define domain root as a constant

define('DOM_ROOT','/var/www/path/to/example.com');

or for small systems,

include_once(('/var/www/path/to/example.com/inc/include.php');

OR use environment variables.

include_once(DOM_ROOT . "/include.php");

Where people get confused is they continue to struggle with the relative url/path, and they look so much the same, begin to think they are the same thing. They are not.

* Why not full URL? When you go to replicate pages/code, you can nit-pic around doing a search and replace of the full URL's in your documents, or you can just MOVE them without messing with it. /path/to/css/style.css can move to example.com or example2.com and still work, with the full URL you have to change all your files.

andrewsmd

7:12 pm on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks