Way to change include path in PHP?

Entity

Lifer
Oct 11, 1999
10,090
0
0
I'm starting work on a large modular script in PHP, and am running into some problems with includes...here's my directory structure right now:

webserver/script/lib -> libraries (classes, functions, and the authorization script)
webserver/script/config -> configuration files (headers, footers, and the config.inc file itself)

I'd really like to have an easier way to include everything I want than having to type include("http://webserver/script/lib/functions/dbfunctions.inc") whenever I want to include that particular set of functions. I've looked around but haven't found much advice. Any would be appreciated. :eek:

thanks,
Rob
 

Czar

Lifer
Oct 9, 1999
28,510
0
0
like this

page you work on in /sitething/
functions you have in /functions/

include("../functions/function.php");
 

Entity

Lifer
Oct 11, 1999
10,090
0
0
Yeah, Czar, I knew how to have includes, I just wanted to make it easier to type when I wanted to have an include. This should work, though...

(in config.inc)

$dbfunctions = "../lib/functions/dbfunctions.inc"

Then, when I want to call it, I can just include $dbfunctions;

Rob
 

Czar

Lifer
Oct 9, 1999
28,510
0
0
that should work, just do
include($dbfunctions);
instead

could also create a function in your config file that includes the functions :)
 

Entity

Lifer
Oct 11, 1999
10,090
0
0
Thanks. I got it (finally). :D

Rob (struggling with his first "real" language, even though PHP is easier than most stuff)
 

Czar

Lifer
Oct 9, 1999
28,510
0
0
glad you choose php :)

also if you want quick expert advice I suggest IRC on #php. I started learning php with reading one tutorial and the rest I'v learned is just from the people on irc and from the php manual :)
 

GagHalfrunt

Lifer
Apr 19, 2001
25,284
1,997
126
It's best, when dealing with includes, to have at least 2 include paths set. It's easiest to have the includes in the directory with the files that use them since that's where PHP looks for them. So you'd just:

include("somefile.inc");

There are of course a few instances where you might want to include some functions in lots of different scripts. So you have the option of using:

include("../path/to/somefile.inc");

But there's an easier way. Set up your .htaccess to use multiple include paths just like the old DOS path line in the config.sys file

In PHP3 it's:
php3_include_path .:/path/to/include/directory

or even
php3_include_path .:/path/to/include/directory:/path/to/a/different/directory:/path/to/a/3rd/directory etc etc etc

You can have as many directories as you want, just seperate them with a colon. Start with a . to reference the current directory

In PHP4 the syntax is:
php_value include_path .:/path/to/include/directory

So any file located in any directory mentioned in the php_value include_path statement can be included with a simple:
include("somefile.inc");

You don't have to use the path each time.


For security purposes, it's best to set up an include directory outside of the publicly viewable tree and use that directory for storing all the files that deal with passwords, database connection functions, etc. That way even if the server hiccups and your directory structure gets readable temporarily that directory is secure since it's outside the viewable tree.