0

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.

1
  • Absolute path is relative :) You need an absolute path taking your webroot as base path, not your filesystem. /include is absolute. Commented Jan 6, 2010 at 19:26

6 Answers 6

9

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Note that 'include/style.css' is also a relative url to the subdirectory include from the current directory.
I need to use absolute paths. This saves me the trouble of putting an 'include' folder in every subdirectory of the project, no?
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.
@wretrOvian: Technically, the first url is absolute. To clarify, the webserver's / uri is the file system's $_SERVER['DOCUMENT_ROOT'].
@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.
|
1

Try using:

$_SERVER['HTTP_HOST']

Comments

1

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.

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.