Jump to content

why $_SERVER['DOCUMENT_ROOT']; sometimes works??


samoht

Recommended Posts

hello again,

 

I would like to be able to use

$docRoot = $_SERVER['DOCUMENT_ROOT'];

 

to define my path fully when linking pages. for some reason this works fine on the live server, and fine on my laptop (which uses wamp and the server name is localhost) but on my work test server )that is networked) the path always shows a C:// etc

 

Why is this ??

And can I fix-it??

Link to comment
https://forums.phpfreaks.com/topic/69667-why-_serverdocument_root-sometimes-works/
Share on other sites

Hey there,

 

if you are trying to use the document root for pages you will run into trouble every time. The document root refers to actual root defined in the server configuration. Using the document root is helpful for includes and file commands in php (among many other things), just not html linking.

<a href="/example.php">example</a>

 

the above code should go to your web root (meaning the folder that is available for the users to see). if this doesn't get you directly to your root, then the server may need a variable to be set. usually these are set your environment vars.

 

ref: http://us.php.net/getenv

 

good luck

the problem is that I have some nested pages and I don't want to

have "../../../"

 

but would much rather have a variable that starts the path correctly,

 

so if live is www.live.com/

 

local(my laptop) is localhost/clientname/html/

 

and work is workserver/Clients/clientname/html/

 

I could just put the variable in to work for each case. I know I can set up a variable in a switch case exactly like this, but then for each client I have to physically change the name - it would be nice to just be able to use document root or something that fills in the first part of the url no matter what machine I am on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.