19

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?

7 Answers 7

16

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.

7

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.

5

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.

1
  • php_value include_path "/var/www/somesite.com/include_path/"
    – velcrow
    Dec 12, 2012 at 18:11
3

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

1
  • 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.
    – AnnanFay
    Oct 3, 2010 at 13:33
1

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.

1

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.

0

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 :)

3
  • Why are people dissing @Gary's answer? A bad practice here at SO in my opinion - downvoting comments out of ones "limitless wisdom" without explaining it in the comments section. Sep 10, 2012 at 14:53
  • Meh. That's PHP for you. I'm still using this system and it works great with APC performance wise. If you can, use an autoloader. The problem with an auto loader is you have to bastardize your class names if you want to have any sort of structure to your class layout on the file system (for example php.net/manual/en/language.oop5.autoload.php#49496). I'd love to hear better options! Sep 11, 2012 at 15:47
  • Gary, i merely pointed out how people tend to downvote stuff without commenting on it. As far as I am concerned your answer is legitimate. Sep 12, 2012 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.