I have a couple of pages on the root folder. There is a folder called “includes” in the root. In the includes folder, I have a couple of files like, db.php (for db connection) and functions.php (to store some common functions etc).
In the front page files, I put this code:
include_once("includes/functions.php");
.
.
.
other codes here
In the FUNTIONS.PHP I have this line
include_once("db.php");
Now the problem is when I try to load the front page, it shows DB login failure issue.
But, if I put following in the front page, then it works.
Please guide me why the first example is not working ? since the db.php is included in functions.php - it should work that way. Is there any server setting that need to be set ?
I think include paths are relative to the script that is currently running, which is why including db.php from functions.php isn’t working. Try using an absolute path in your include statement from functions.php:
fretburners solution is the way to go here. This allows for includes to work in a file that has been included by another path. Ex:
Assuming /var/www/lib/foo.php
/var/www/lib/file.php:
include 'foo.php';
Running file.php will work, because foo.php is in the same directory as file.php. However:
/var/www/index.php:
include 'lib/file.php';
Include foo.php will now fail. The current path of the script is now /var/www/ and will look for foo.php in that folder as well as any php.ini defined inlude folder.
dirname(FILE) will provide the actualy file path. I use this for everything.