I recently joined Host Gator. I was surprised that they have their system configured differently to my previous host. At Host Gator, $_SERVER[‘DOCUMENT_ROOT’] points to the folder one up from the actual folder that my site is in, a directory named after my chosen username.
I use MAMP for local development and reflected the remote site’s structure so that I won’t have to change code for live deployment. However my local server correctly points to ‘[username]’ when commanded from $_SERVER[‘DOCUMENT_ROOT’].
Host Gator tells me that they can do nothing about it because it is a shared folder. My only recourse is to mimic Host Gator’s behaviour locally.
How do I do that? I suspect that it would involve editing config files etc. and I’m fine with that.
Also, if it is possible, can this be done PER SITE? Or would it affect all local sites?
You could just avoid using $_SERVER[‘DOCUMENT_ROOT’] entirely.
Or manually set it at some point early in your script, in a file which you know will be in the root of the site… something like
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
so that the behavior is consistent between your servers.
dirname(FILE); should handle the situation correctly.
I solved the problem.
$_SERVER['DOCUMENT_ROOT'] = str_replace("/admin","",$_SERVER['DOCUMENT_ROOT']);
On my local machine, the username directory is correctly removed from the pathname. On the remote server, since the username directory is not found in the pathname, $_SERVER[‘DOCUMENT_ROOT’] returns itself.
Wouldn’t a solution which is portable and works everywhere be preferable to one which depends on the username under which a script runs?