Reputation: 9759
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?
Upvotes: 19
Views: 25019
Reputation: 3948
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.
Upvotes: 5
Reputation: 16441
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 :)
Upvotes: 0
Reputation: 23840
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
andPHP_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.
Upvotes: 17
Reputation: 16383
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
Upvotes: 3
Reputation: 943
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.
Upvotes: 1
Reputation: 505
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.
Upvotes: 7
Reputation: 180126
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.
Upvotes: 1