tags:

views:

6540

answers:

8

I can set the php include path in the php.ini:

include_path = /path/to/site/includes/

But then other websites are effected so that is no good.

I can set the php include in the start of every file:

$path = '/path/to/site/includes/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

But that seems like bad practice and clutters things up.

So I can make an include of that and then include it into every file:

include 'includes/config.php';

or

include '../includes/config.php';

This is what I'm doing right now, but the include path of config.php will change depending on what is including it.

Is there a better way? Does it matter?

+10  A: 

If you're using apache as a webserver you can override (if you allow it) settings using .htaccess files. See the php manual for details.

Basically you put a file called .htaccess in your website root, which contains some php ini values. Provided you configured apache to allow overrides, this site will use all values in your php config, + the values you specify in the .htaccess file.

"Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives", as stated in the page I linked. If you click through to the full listing, you see that the include path is a PHP_INI_ALL directive.

Erik van Brakel
A: 

Generally with my systems I use a DIR constant which is gwtcwd(). This is called in construct.php (a page startup file). I agree that include_path is probably a more concrete solution but I still use my DIR constants out of habit.

Ross
A: 

You can set include_path in your php.ini file too. I'm a perl guy, so I expect to be able to load includes and have include do the right thing. I have all my includes in a specific directory, which is added to include_path. I can do things like

require_once "ClassName.php";

I don't need to worry about relative paths or locations of files.

I've also written my own CustomRequire to do things like

function CustomRequire ($file)
{
  if(defined('MYINCLUDEPATH'))
  {
    require_once MYINCLUDEPATH . "/$file";
  }
  else
  {
    require_once $file;
  }
}

That way I can change how I do includes at a later date. Of course, you still need to find a way to include your include code :)

Gary Richardson
+1  A: 

Depending on how your host is set up, you may be permitted to place a php.ini file in the root of your home directory with extra configuration directives.

ceejayoz
+5  A: 

Erik Van Brakel gave, IMHO, one of the best answers.

More, if you're using Apache & Virtual hosts, you can set up includes directly in them. Using this method, you won't have to remember to leave php_admin commands in your .htaccess.

Pierre-Yves Gillier
+1  A: 

Use a php.ini file in website root if your setup uses PHP as CGI (the most frequent case on shared hosts) with the same syntax as the server-wide php.ini; put it into .htaccess if you have PHP as an Apache module (do a phpinfo() if unsure):

php_value include_path "wherever"

Note that per-folder php.ini does not affects subfolders.

djn
A: 

Your application should have a config file written in PHP. Then include that with a relative page into every page in the program. That config file will have a variable for the path to the includes dir, templates dir, images dir, etc.

Sherri
A: 

Why do you think append to include path is bad practice?

This code near top of root script shouldn't be that bad...

$path = '/path/to/site/includes/';
set_include_path($path . PATH_SEPARATOR . get_include_path());

IMHO the main advantage is that it's portable and compatible not only with Apache

EDIT: I saw a drawback of this method: small performance impact. see http://www.geeksengine.com/article/php-include-path.html

Double Gras
Apart from the performance impact you mention, when I asked this question I was working on a site with many separate files and not a single root script which all requests are directed to.
Annan