PHP include issue


I am user in local WAMP server.

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:

other codes here

In the FUNTIONS.PHP I have this line


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 ?


Hi ZH,

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:

include_once dirname(__FILE__) . '/db.php';

you will need to use



as you are including the function.php on your file on root, so when you code


is included it looks for that file on root, so you need to specify the path also.

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


include 'foo.php';

Running file.php will work, because foo.php is in the same directory as file.php. However:


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.

dirname(FILE) can actually be shortened to DIR

include DIR .‘/db.php’;

The simpler solution is to include the following at the top of the script:

set_include_path(get_include_path() . PATH_SEPARATOR . 'my/custom/path');

where my/custom/path is the path to the folder where all the include files can be found.

Then you don’t need to specify a path on the front of any include filenames.

On the other hand, it means that your include file names need to be globally unique.

Very valid point.

Thanks every one !

@fretburner, @felgall - your solutions working…