在每个站点的基础上设置 PHP 包含路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:12:46  来源:igfitidea点击:

Setting PHP Include Path on a per site basis?

phpinclude

提问by AnnanFay

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

我可以在以下位置设置 PHP 包含路径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:

我可以在每个文件的开头设置 PHP 包含:

$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.phpwill change depending on what is including it.

这就是我现在正在做的事情,但是 的包含路径config.php将根据包含它的内容而改变。

Is there a better way? Does it matter?

有没有更好的办法?有关系吗?

采纳答案by Erik van Brakel

If you're using apache as a webserver you can override (if you allow it) settings using .htaccessfiles. See the PHP manualfor details.

如果您使用 apache 作为网络服务器,您可以使用.htaccess文件覆盖(如果您允许)设置。有关详细信息,请参阅PHP 手册

Basically you put a file called .htaccessin your website root, which contains some PHP inivalues. Provided you configured Apache to allow overrides, this site will use all values in your PHP config, + the values you specify in the .htaccessfile.

基本上,您在网站根目录中放置了一个名为.htaccess的文件,其中包含一些 PHPini值。如果您将 Apache 配置为允许覆盖,该站点将使用您的 PHP 配置中的所有值,以及您在.htaccess文件中指定的值。

Can be used only with PHP_INI_ALLand PHP_INI_PERDIRtype directives

只能与PHP_INI_ALLPHP_INI_PERDIR类型指令一起使用

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_ALLdirective.

如我链接的页面所述。如果您点击进入完整列表,您会看到包含路径是一个PHP_INI_ALL指令。

回答by Pierre-Yves Gillier

Erik Van Brakel gave, IMHO, one of the best answers.

恕我直言,Erik Van Brakel 给出了最好的答案之一。

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.

此外,如果您使用 Apache 和虚拟主机,则可以直接在其中设置包含。使用这种方法,您不必记住将 php_admin 命令留在您的 .htaccess 中。

回答by djn

Use a php.inifile in website root, if your setup uses PHPas CGI(the most frequent case on shared hosts) with the same syntax as the server-wide php.ini; put it into .htaccessif you have PHP as an Apache module (do a phpinfo()if unsure):

在网站根目录中使用php.ini文件,如果您的设置使用PHP作为CGI(共享主机上最常见的情况),其语法与服务器范围的php.ini 相同;如果您将 PHP 作为 Apache 模块,请将其放入(如果不确定,请执行):.htaccessphpinfo()

php_value include_path "wherever"

Note that per-folder php.ini doesnot affects subfolders.

请注意,每个文件夹php.ini does不会影响子文件夹

回答by Gras Double

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

恕我直言,主要优点是它不仅可移植且与 Apache 兼容

EDIT: I saw a drawback of this method: small performance impact. see http://www.geeksengine.com/article/php-include-path.html

编辑:我看到了这种方法的一个缺点:对性能的影响很小。见http://www.geeksengine.com/article/php-include-path.html

回答by ceejayoz

Depending on how your host is set up, you may be permitted to place a php.inifile in the root of your home directory with extra configuration directives.

根据您的主机的设置方式,您可能会被允许php.ini在您的主目录的根目录中放置一个带有额外配置指令的文件。

回答by Sherri

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.

您的应用程序应该有一个用 PHP 编写的配置文件。然后将其与相关页面一起包含到程序中的每个页面中。该配置文件将有一个变量,用于包含目录、模板目录、图像目录等的路径。

回答by Gary Richardson

You can set include_pathin your php.ini file too. I'm a perl guy, so I expect to be able to load includes and have includedo the right thing. I have all my includes in a specific directory, which is added to include_path. I can do things like

您也可以include_path在 php.ini 文件中进行设置。我是一个 perl 人,所以我希望能够加载包含并include做正确的事情。我的所有包含都在一个特定的目录中,该目录被添加到include_path. 我可以做类似的事情

require_once "ClassName.php";

I don't need to worry about relative paths or locations of files.

我不需要担心文件的相对路径或位置。

I've also written my own CustomRequireto do things like

我也写了自己CustomRequire的来做类似的事情

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

这样我就可以在以后改变我的处理方式。当然,您仍然需要找到一种方法来包含您的包含代码:)