I'm having some trouble with include, and I'm not sure if there's a way around it.
Here's a simplified version of my directories and files:
test.php
actions/login.php
classes/config.php
classes/database.php
My config.php includes database.php.
When I try to have test.php include config.php, config.php can't find database.php because it's looking in the directory that test.php is in, not it's current directory.
Is there some way around this? I realize that I can just have test.php do the database.php include before I include config.php, but I was hoping to avoid doing that.
Thanks for any suggestions,
Jesse
php includes
Moderator: General Moderators
-
jessevitrone
- Forum Newbie
- Posts: 15
- Joined: Tue Aug 12, 2003 2:42 pm
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
-
Yes there is, you can try $ _SERVER["DOCUMENT_ROOT"],
But it has some disadvantage, because it's behaviour is different on each server. Sometimes it returns
e.g
/var/www/user/~username/
sometimes only
/~username/
,so you will run in trouble than again.
That is my experience with document_root.
Another way is to set a global variable, that holds if you include from root or from upper folder and adjust the path then with an if clause.
like
Not very nice, but that is safe on all servers.
djot
-
Yes there is, you can try $ _SERVER["DOCUMENT_ROOT"],
But it has some disadvantage, because it's behaviour is different on each server. Sometimes it returns
e.g
/var/www/user/~username/
sometimes only
/~username/
,so you will run in trouble than again.
That is my experience with document_root.
Another way is to set a global variable, that holds if you include from root or from upper folder and adjust the path then with an if clause.
like
Code: Select all
<?php
// include from root (./)
if ($yourglobalvar == "root") { include_once('./classes/config.php'); }
// include from e.g. ./actions/
else { include_once('../classes/config.php'); }
?>djot
-
Last edited by djot on Tue Oct 12, 2004 8:39 am, edited 3 times in total.
-
jessevitrone
- Forum Newbie
- Posts: 15
- Joined: Tue Aug 12, 2003 2:42 pm