我可以在以下位置设置PHP包含路径php.ini:
include_path = /path/to/site/includes/
Run Code Online (Sandbox Code Playgroud)
但其他网站受到影响,这是不好的.
我可以在每个文件的开头设置PHP include:
$path = '/path/to/site/includes/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
Run Code Online (Sandbox Code Playgroud)
但这似乎是不好的做法,并且使事情变得混乱.
所以我可以将其包含在内,然后将其包含在每个文件中:
include 'includes/config.php';
Run Code Online (Sandbox Code Playgroud)
要么
include '../includes/config.php';
Run Code Online (Sandbox Code Playgroud)
这就是我现在正在做的事情,但是包含路径config.php将根据包含的内容而改变.
有没有更好的办法?有关系吗?
小智 7
恕我直言,Erik Van Brakel给出了最好的答案之一.
此外,如果您使用的是Apache和虚拟主机,则可以直接在其中设置包含.使用此方法,您不必记住在.htaccess中保留php_admin命令.
在网站根目录中使用php.ini文件,如果您的设置使用PHP作为CGI(共享主机上最常见的情况),其语法与服务器范围的php.ini 相同;如果您将 PHP 作为 Apache 模块,请将其放入(如果不确定,请执行):.htaccessphpinfo()
php_value include_path "wherever"
Run Code Online (Sandbox Code Playgroud)
请注意,每个文件夹php.ini does不会影响子文件夹。