tags:

views:

906

answers:

6

Hi. I'm trying to link to a stylesheet in my header file using $_SERVER["DOCUMENT_ROOT"] as follows:

<head>
    <?php
        print "<link href='".$_SERVER["DOCUMENT_ROOT"]."/include/style.css' rel='stylesheet' type='text/css' />";
    ?>
    <title>eLMS</title>

</head>

Since i'm testing locally, i'm getting the path as:

<head>
    <link href='C:\Users\wretrOvian\Documents\eLMS\site/include/style.css' rel='stylesheet' type='text/css' />        <title>eLMS</title>

</head>

And this is not rendering in Firefox. It does in IE however. This is obviously a validation issue. BUT, i tried the same code on a server - with the same results. :(

How do i go about fixing this? The end product may or may not run on a Local server, so i need the code to be flexible..

I'm using Abyss Webserver x1 with PHP 5.2.8

I must use absolute paths - because i do not want to copy the include folder to every subdirectory of the app. I need to be able to refer to it from every location.

A: 

For using local files from the browser, use the file scheme.

file://C:/dir/file.ext
file:///dir/file.ext

Not sure if you need two or three slashes, probably two on windows with the drive letter, three on *nix with the root slash, though I seem to recall seeing three slashes with the drive letter. Try it! :P

Tor Valamo
+8  A: 

Just use this:

<head>
    <link href='/include/style.css' rel='stylesheet' type='text/css' />
    <title>eLMS</title>
</head>

Or, if it is used locally:

<head>
    <link href='../include/style.css' rel='stylesheet' type='text/css' />
    <title>eLMS</title>
</head>

The document root is for internal usage (inside of PHP) only, not for in your HTML.

Jimmy Shelter
Note that `'include/style.css'` is also a relative url to the subdirectory include from the current directory.
R. Bemrose
I need to use absolute paths. This saves me the trouble of putting an 'include' folder in every subdirectory of the project, no?
wretrOvian
You can go back a number of folders by adding more ../.. (The directory where you put your css includes MUST be available from the document root, you can't put it below it.
Jimmy Shelter
@wretrOvian: Technically, the first url is absolute. To clarify, the webserver's `/` uri is the file system's `$_SERVER['DOCUMENT_ROOT']`.
R. Bemrose
@Bemrose: Agreed, but this is an isolated case. I highlight again that if i'm in a deep subdirectory - i will have to modify the code for every page - AND if my header links to the stylesheet - i'll have to use a custom header for every subdirectory.
wretrOvian
@wretrOvian: `$_SERVER['DOCUMENT_ROOT']` does not change when you go into deeper subdirectories. It's still whatever you set the document root to in your web server's configuration.
R. Bemrose
By using just /include, you should be able to access the include directory from every other subdirectory.
Jimmy Shelter
@wretrOvian: His first suggestion is to use `/include/style.css` which will always map to `C:\Users\wretrOvian\Documents\eLMS\site\include\style.css` If you want a relative root directory in a url, you need to use `./`
R. Bemrose
@Bemrose: From any sub-subdirectory whatsoever, are you sure? :/
wretrOvian
@Bemrose, @Jimmy: Got it. Thanks :]
wretrOvian
Yes; root-relative paths are absolute. And are consistent through the entire site (since they're relative to the web-root, which doesn't change).
David Thomas
A: 

Try using the DIRECTORY_SEPARATOR constant. It returns \ on Windows systems and / on *nix systems.

mabwi
A: 

Try using:

$_SERVER['HTTP_HOST']
danielrsmith
A: 

First of all, the document root is literally the directory that the web server's / is located at. You do not usually want to use this with any content on the web. Use / instead.

R. Bemrose
+1  A: 

Don't use a filesystem absolute path, use a path relative to (but not including) the document root. In this case just /include/style.css.

kemp