Page 1 of 1

Problems using include() from various directory levels

Posted: Thu Oct 02, 2003 2:44 pm
by Swede78
This question has already been asked on this forum, but I don't think the answer looked for was ever given.

I've created an auto-login script which I want to include in every possible entry page on my site. The problem that I'm having is that the auto-login script may be called from different directory levels. So, I've tried different ways of calling the include file:

In this example, say it's being called from
http://www.domain.com/A/test.php or
http://www.domain.com/A/subpath/test.php

include ('/B/file.php');
include ('B/file.php');
include ('./B/file.php');
include ('http://www.domain.com/B/file.php');

None of those work.

My site is on a Win2K machine with IIS. I've read that I can change include_path settings in the php.ini file, but what if I don't have access to it? Is that the only way to include a relative-path file?

I could go into every file and make sure I get the include file path prefix set manually, such as '../' or '../../'. This is what I'm trying to avoid doing. I already have a header file being included in every file, so it would be nice to add this include to just the header file. You'd think it'd work like calling an img src where you can just use img src="/images/whatever.gif".

Any help would be greatly appreciated!

Posted: Thu Oct 02, 2003 3:13 pm
by Cruzado_Mainfrm
you can see in what level you are by using the phpinfo() function...

Posted: Thu Oct 02, 2003 3:27 pm
by jason
Several ways to accomplish this. Either set your include_path to include the path of the file that you want to easily include. So if the dir structure looks like this:

/home/site/public_html/index.php
/home/site/inc/config.php
/home/site/public_html/subdir/file.php

You make /home/site/inc a path in your include_path, and simply do:

Code: Select all

include 'config.php';
If that's not an option, you could also just give the entire path:

Code: Select all

include '/home/jason/inc/config.php';
That will also work.

Posted: Thu Oct 02, 2003 4:12 pm
by Swede78
jason wrote:If that's not an option, you could also just give the entire path:

Code: Select all

include '/home/jason/inc/config.php';
That will also work.

Ok, I see... you have to include more than your website's root, you have to go further back. Since, I'm using IIS, I tried the following which seems to work.

include ('/Inetpub/sitename/inc/file.php');

Thank you!!!

Posted: Thu Oct 02, 2003 4:32 pm
by Swede78
This just made me think of another question...

If I was able to get into the php.ini file and add to the include_path, what would happen in the following scenerio:

Let's say I have 2 websites:
SiteABC located in /Inetpub/SiteABC/
SiteDEF located in /Inetpub/SiteDEF/

And in each site dir, there is a directory called "includes" both with a file called "autologin.php".

If I include "autologin.php" from SiteABC, will PHP know to look in SiteABC/includes, before looking in SiteDEF/includes?