0

I use $_SERVER[DOCUMENT_ROOT] a lot to make sure that all my links will work properly. For instance:

<link href="' . $_SERVER[DOCUMENT_ROOT] . '/favicon.ico" rel="icon" />

It doesn't matter the actual path of the file that's in, it will always find the correct location of the favicon relative to the document root.

This all works as expected on my web server. However, on my local development server (Windows 7, Apache 2.2, virtual hosts configured), while it points to the correct path (C:/Local/MySite), nothing actually happens (favicon doesn't work, stylesheets and images don't get loaded, etc.)

Is there any way of getting $_SERVER[DOCUMENT_ROOT] to work locally?

I found that I can get the same expected result by using "http://" and $_SERVER[HTTP_HOST] (<link href="http://' . $_SERVER[HTTP_HOST] . '/favicon.ico" rel="icon" />), is there any downside to doing that? Alternatively, I can continue to use $_SERVER[DOCUMENT_ROOT] and just use server rewrite rules to parse it into something that will run locally, and leave it untouched for the browser.

2 Answers 2

3

$_SERVER[DOCUMENT_ROOT] is the entire path to the website's root directory and should fail spectacularly for links and images.

Now, having said that, what you have so far is OK... except that $_SERVER[HTTP_HOST] only works if the connection is made using HTTP/1.1 and the client issues a Host header. $_SERVER[SERVER_NAME] should always work, though. Just be aware that $_SERVER[SERVER_NAME] will use the virtual hosts main address, so if a server has aliases...

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

5 Comments

Do you mean to say that $_SERVER[DOCUMENT_ROOT] shouldn't be used at all, whether local or not?
Yes, don't use $_SERVER[DOCUMENT_ROOT] in links/images. It does have its uses, though, such as when you're using PHP to copy/move files around.
$_SERVER["DOCUMENT_ROOT"] should be used only for script's internals (requires, includes...), but should not be used to externals (html, images...)
Ah, OK got it. I Just realized that $_SERVER[DOCUMENT_ROOT] seems to be working fine for includes, so I assume that use is OK? EDIT: Thanks @StormByte
@PixelElephant Yes, using it for includes is fine.
1

The document root is a local path on your server, and has no meaning whatsoever in URL space as far as browsers see your site.

If your html has

<img src="c:/site/html/favicon.ico" />

you're tell the user's browser to try and load that favicon from the user's own local drive. Not only will this not work, it also assumes that the user is on a Windows machine. Mac/Unix/Linux users won't have a C: drive, let along a /site/html directory tree in the root of their own local file system.

$_SERVER['DOCUMENT_ROOT'] is used for in-server local file operations, e.g. fopen(), file_get_contents(), etc... But is essentially totally useless for client-side operations.

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.