tags:

views:

157

answers:

3

I am using document root to provide absolute path which is not working. if i echo this path it turns out to be C:wamp/www/proman/header.php. I i give relative path it works fine what is the problem here?

$path = $_SERVER['DOCUMENT_ROOT']."proman/header.php";

I elaborate my problem here: I have 2 php files data_object.php and user.class.php. user.class.php has an include statement for data_object.php wchih is relative to user.class.php.These two files are under different directory hierarchy. Now I have to include this user.class.php in various files (like projects.php, links.php) under different hierarchy when i want to create a User() object. The problem is the relative path for file inclusion of data_object.php does work for say projects.php but if i open links.php the error message says it could not open file data_object.php in user.class.php. What i think is for relative inclusion of data_object.php it is considering the path of the file in which user.class.php is included. I am facing such problems in more than one scenarios I have to keep my directory structure the way it is but have to find a way to work with nested includes. I am currently running on wamp server but after completion i have to host the solution on a domain. Pls help

A: 

Your question is rather unclear. Are you expecting a path you can use in HTML? If so, DOCUMENT_ROOT is the wrong thing to use - you want something like REQUEST_URI or SCRIPT_NAME.

ceejayoz
A: 

Since that's a server variable, you may or may not see it, depending on which web server you're running under (especially IIS), or if something's configured weird.

One way to deal with this problem is to set the variable.

$_SERVER['DOCUMENT_ROOT'] = "C:/some/absolute/path";
// or, if you put this code in a file that's in your document root: 
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);

Then, you can either require() this file when you need to verify the document root, or use the auto_prepend_file php.ini option to include the file automatically.

If you are actually trying to make a URL, then you just specify an absolute URL - /proman/header.php, or a relative URL - ../proman/header.php.

Seth
+1  A: 

In my experience, $_SERVER['DOCUMENT_ROOT'] doesn't including a trailing slash. Try:

$path = $_SERVER['DOCUMENT_ROOT'].'/proman/header.php';
webbiedave