View Full Version : Problems with $_SERVER["DOCUMENT_ROOT"] in a Subdirectory.

12-21-2006, 01:30 AM
Hey, I have a quick question. I've having some problems while working in a subdirectory and using the "DOCUMENT_ROOT" command.

I have a subdirectory (i.e. "http://sub.mysite.com") and when I use a "$_SERVER["DOCUMENT_ROOT"]" in one of the pages (such as "http://sub.mysite.com/dir/content.php") to load, say, a header, the PHP parser recognizes this call as being to "http://mysite.com" instead of the subdirectory from where it is being called.

One method I have used to get around this problem is to use the code "$_SERVER["DOCUMENT_ROOT"].sub" when I write that statement and everything works fine. However, this is quite inconvenient for my site style layout and I'm wondering if PHP has and tricks in it for dealing with and traversing subdirectories.

Another method that I have found that works is to use "$_SERVER["SUBDOMAIN_DOCUMENT_ROOT"]", though this is also inconvenient. So, my question is..can PHP work inside subdirectories using default commands like DOCUMENT_ROOT whilst in a subdirectory?

Thanks for your help. =)

12-21-2006, 11:00 AM
You could modify Document_Root with htaccess to suit to your needs

12-21-2006, 11:01 AM
You could modify Document_Root with htaccess to suit to your needs

12-21-2006, 03:59 PM
You could modify Document_Root with htaccess to suit to your needs

Where and how would I do that? Thanks for your replies.

I've heard about and even used .htaccess in the past, but never really kept using it as I found out about PHP and coded my own login page that way. So how would .htaccess be used then?

12-21-2006, 04:29 PM
Note the difference between subdomains and subdirectories...
sub.domain.com , is a subdomain
www.domain.com/dir/ or sub.domain.com/dir/ are both subdirectories of the root folder of subdomain (www is just another subdomain)

your DOCUMENT_ROOT should be a filepath /home/httpd/etc/etc it should have nothing to do with http://etc , its hard to answer your question since different hosts handle subdomains in different ways, some control panels create the subdomain as a subdirectory of the root domain, others in a separate directory altogether.

If SUBDOMAIN_DOCUMENT_ROOT is available then I would have thought that would be the answer (assuming it is also set when in the www subdomain)

you could also perhaps look at getpwd() etc to find out where you are in the filesystem and work it out from there.

12-28-2006, 08:23 AM

I am having a similar issue with a pre-built php script that uses "$_SERVER["DOCUMENT_ROOT"]" to include files.

My server panel configures subdomains as a subdirectory of the root, and as a result the document_root of the subdomain resolves to the main url document_root.

I cannot change the php script and the developper tells me that I need to get my subdomain configured as a seperate directory so that the references to include files with "$_SERVER["DOCUMENT_ROOT"]" will find files when installed in the subdomain.

Instead of changing server setup, how do I get the subdomain DOCUMENT_ROOT to point to the subdomain base directory in .htaccess so that the script can find all include files ?

Can I do the same with SITE_HTMLROOT if at all possible in .htaccess ?

Thank You