Web Hosting Talk







View Full Version : $_SERVER['DOCUMENT_ROOT'] returning wrong string


PMichaud
09-27-2004, 05:00 PM
I've been programming an application for several months on a server, and I've recently switched to hostgator.com

The includes don't work on this new server because it seems that the DOCUMENT_ROOT value in the Server php super global is set wrong.

It returns: /usr/local/apache/htdocs

when I try PATH_TRANSLATED for debugging purposes it returns: /home/spider/public_html/index.php


I need for DOCUMENT_ROOT, then, to be returning /home/spider/public_html (as it did on my old server). What can I do to fix this problem?

thartdyke
09-27-2004, 05:08 PM
Try setting doc_root in a .htaccess file

PMichaud
09-27-2004, 07:08 PM
I will try that, but I'm not sure how (through the control panel, right) and I'm not sure about the syntax in htaccess files.

Can you be a little more specific?

thartdyke
09-27-2004, 07:59 PM
Er, no, not really, not off hand :stickout: To be honest, I'm not sure if that's a directive that can be set from a .htaccess file.

You could always try using this from PHP:
ini_set('doc_root', '/home/spider/public_html');

PMichaud
09-27-2004, 08:10 PM
I've contacted support about it -- if there's a way to make it work without altering the code, I'd rather do it that way... right now the script ( 15k+ lines of code) is just for this site, but I'm planning to make it totally portable so I can clone the site concept, and I'd like to have code that is totally portable.

Thanks for the help though,
Pete

Burhan
09-28-2004, 02:40 AM
Its returning the correct value, as per PHP docs:

" The document root directory under which the current script is executing, as defined in the server's configuration file."

PATH_TRANSLATED, is what you really want:

" Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping."

It seems that their server is configured differently than your previous host. You should always use PATH_TRANSLATED, not DOCUMENT_ROOT. Two different variable, that are confusing because on some setups they return the same value.

PMichaud
09-28-2004, 02:07 PM
Ok, that's good to know but the path translated returns all of it, including the filename itself, which makes it impractical to use for, example, including files as in:

require $_SERVER['PATH_TRANSLATED'] . '/folder/include.inc';

that would return, perhaps, 'home/user/public_html/index.php/folder/include.inc'

when I'd actually need something without the filename in the middle. What is the variable I want to call for that?

hiryuu
09-28-2004, 05:50 PM
The dirname() will chop off the filename portion of the path, which will get you closer to what you need. Is there a reason you use the full path for the includes? Something relative (require 'includes/lib.php';) would allow you to move the whole structure without issues.

Also make sure the web server doesn't return .inc files as text. You should keep the .php extension on your included files, so they just execute and return a blank page.

PMichaud
09-28-2004, 06:06 PM
Yes, there is a reason:

the header.php file includes my global include for all the functions and whatnot I use (there are many), and this header is included by files on many different directory levels... so if I set up the header to work fine for index.php, then it'll break for folder/index.php -- I need it to work for both, so I need an absolute path.

Further, I can't hardcode the path because this is an application that, when I'm done writing it, I'm planning to distribute to other sites, so it has to be portable.

I can't be the only one with this problem -- does anyone know what the proper coding is?

hiryuu
09-28-2004, 10:14 PM
If they're global includes, you can add the necessary directories to your include_path (colon-separated) so PHP will automatically look there.

PMichaud
09-28-2004, 10:23 PM
that's exactly what I've done, so it looks like we're on the same page.

One other related problem though... now that includes work, what about the images? They need the same absolute path treatment, but they aren't includes, so the server isn't looking for them in a given directory. Is there an htaccess line I can add to make files look for images in a certain directory?

hiryuu
09-29-2004, 07:27 AM
I assume that's to create/remove/manipulate the images with GD and such? There's nothing as integral as the include path, but you can use Apache/.htaccess 'SetEnv' to set values outside the scripts. You may just want to include a config.php file if you have more than a couple variables you want to push.