cancel
Showing results for 
Search instead for 
Did you mean: 

$_SERVER inconsistencies

kitsbury
Newbie
Posts: 3
Registered: ‎06-04-2010

$_SERVER inconsistencies

It seems that some of the paths in the $_SERVER global are inconsistent.
In particular $_SERVER[DOCUMENT_ROOT] is set to "/var/www", which isn't much use for deriving absolute paths within the "user space allocation".
are the following paths equivalent?
from $_SERVER[SCRIPT_FILENAME],
/files/home2/username/
from $_SERVER[SCRIPT_NAME], $_SERVER[PHP_SELF]
/~username/
ideally $_SERVER[DOCUMENT_ROOT] should reflect the path for the users space allocation. alternatively (as in the previous CGI platform implementation) an additional value was included in the $_SERVER global: [C_DOCUMENT_ROOT]
The current situation is not really satisfactory.
4 REPLIES 4
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: $_SERVER inconsistencies

Quote from: kitsbury
are the following paths equivalent?
from $_SERVER[SCRIPT_FILENAME],
/files/home2/username/
from $_SERVER[SCRIPT_NAME], $_SERVER[PHP_SELF]
/~username/

No they are not. The first one is the full path and name of the script; the others are the script name and path relative to the user's document root. The presence of /~username/ in the latter variables is associated with mod_rewrite (I think).
The environment variables currently set are associated with the security model, possibly can be changed in time but that remains to be seen.
$_SERVER['C_DOCUMENT_ROOT'] (customer document root) is not a standard variable and as you mentioned isn't defined on the new server. The solution is to create your own. For Perl
Quote from: Ben
Or you could add this near the top of your script:
($ENV{'C_DOCUMENT_ROOT'} = $ENV{'SCRIPT_FILENAME'}) =~ s/$ENV{'SCRIPT_URL'}/\//;

It'll be a bit more portable that way.

which strips SCRIPT_URL from SCRIPT_FILENAME. Equivalent code could be used for PHP. However SCRIPT_URL is not an "always present" variable and, for instance, it isn't defined on PAYH. The most portable solution is probably to strip ~username/ (if present) from SCRIPT_NAME, then strip the resulting string from SCRIPT_FILENAME to get the user's document root.
David
seopaul
Newbie
Posts: 2
Registered: ‎13-04-2010

Re: $_SERVER inconsistencies

hhhmmmm i agree with kitsbury  in my world that server is miss configured... SCRIPT_FILENAME should have the  DOCUMENT_ROOT value in the path...
you could add this code before using the DOCUMENT_ROOT var from where you want the doc root tobe.
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);

maybe raise a bug for this as /var/www really is not your doc root it should be /files/home2/username/ or something like
Ben_Brown
Grafter
Posts: 2,839
Registered: ‎13-06-2007

Re: $_SERVER inconsistencies

It's not a misconfiguration as such, it's due to the combination of mod_userdir, suexec and mod_rewrite that we use on the platform.
You can use the 'auto_prepend_file' setting in a custom php.ini file in the directories you have php files in to force all your scripts to read a particular file before executing any code. In the file you specify you can set values for the variables you want to change, and add new ones such as C_DOCUMENT_ROOT.
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: $_SERVER inconsistencies

Just a footnote, but the userdir path is symlinked to the absolute path, so variables that return the absolute path will return paths under /share/storage, not /files. This is useful for back compatibility, but may confuse.
As per
<?php
echo __FILE__."<br>"; //path to file
echo dirname(__FILE__)."<br>"; //path to current directory
echo implode(explode($_SERVER["REQUEST_URI"],__FILE__)); //path to web document root
?>

or
#!/usr/bin/perl -w
print "Content-type: text/plain\n\n";
use Cwd 'abs_path';
use File::Basename;
print abs_path($0)."\n"; #path to file
print dirname(abs_path($0))."\n"; #path to current directory
print join('',split($ENV{'REQUEST_URI'},abs_path($0))); #path to web document root

Gabe