Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting PHP Include Path on a per site basis?

Tags:

include

php

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

include_path = /path/to/site/includes/

But then other websites are affected 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?

like image 682
AnnanFay Avatar asked Aug 23 '08 21:08

AnnanFay


2 Answers

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.

like image 191
Erik van Brakel Avatar answered Nov 04 '22 14:11

Erik van Brakel


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.

like image 7
Pierre-Yves Gillier Avatar answered Nov 04 '22 13:11

Pierre-Yves Gillier