I can set the PHP include path in the php.ini
:
include_path = /path/to/site/includes/
But then other websites are affected so
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.
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 :)
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.
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.
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.
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.