Setting PHP Include Path on a per site basis?

后端 未结 7 1243
既然无缘
既然无缘 2020-12-20 11:25

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

include_path = /path/to/site/includes/

But then other websites are affected so

相关标签:
7条回答
  • 2020-12-20 11:51

    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.

    0 讨论(0)
  • 2020-12-20 11:54

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

    0 讨论(0)
  • 2020-12-20 11:57

    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 讨论(0)
  • 2020-12-20 11:59

    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.

    0 讨论(0)
  • 2020-12-20 12:06

    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.

    0 讨论(0)
  • 2020-12-20 12:10

    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.

    0 讨论(0)
提交回复
热议问题