简体   繁体   English

在公共根目录之外放置配置文件时。 如何在不定义路径的情况下在每个页面上调用它

[英]when placeing a config file outside the public root. how do i call it on every page without defining the path

this is how my file structure is. 这就是我的文件结构。 [..] are folders [..]是文件夹

[public]
[libs]
[crontab]
[logs]
[sessions]
[tmp]

config.php

the domain is set to the public folder as its root. 域被设置为公共文件夹作为其根目录。 everything else including the config.php file is outside the root. 包括config.php文件在内的所有其他内容都在root之外。 the config contains db connection information and other necessary settings. 配置包含数据库连接信息和其他必要设置。

every page in the public folder i have to put the entire path so include the config file. 公共文件夹中的每个页面我必须放置整个路径,所以包括配置文件。 problem is when i move everything from my local machine to the public server i have to go through every page and change the path included for the config file. 问题是,当我将所有内容从本地计算机移动到公共服务器时,我必须遍历每个页面并更改配置文件中包含的路径。 is there a better way? 有没有更好的办法? maybe settings a path in php.ini or something like that? 也许在php.ini中设置路径或类似的东西?

thanks 谢谢

In that specific case you can use: 在这种特定情况下,您可以使用:

include("$_SERVER[DOCUMENT_ROOT]/../config.php");
# Note: special exception array syntax within double quotes.

Which resolves to the public directory first, but moves one level back ../ to reach the config file outside of the docroot. 其中首先解析为public目录,但将一个级别移回../以到达docroot之外的配置文件。


But you could also use SetEnv in your .htaccess to define another $_SERVER environment variable for the occasion. 但您也可以在.htaccess使用SetEnv为场合定义另一个$_SERVER环境变量。

A technique that I frequently use to include files relative to a particular location in your webroot structures is to use an include/require like this: 我经常用来包含相对于webroot结构中特定位置的文件的技术是使用include / require,如下所示:

require_once(dirname(__FILE__) . '/path/to/include.php');

This way, codebase migrations don't need to change any hard-links or constants so long as the files remain relative to each other in the same way. 这样,只要文件以相同的方式保持彼此相对,代码库迁移就不需要更改任何硬链接或常量。

You could just use constant for your path and change it once, and define that constant on every path - included file 您可以只为路径使用常量并更改一次,并在每个路径上定义该常量 - 包含文件

You can also take a look at include path http://php.net/manual/en/function.set-include-path.php 您还可以查看包含路径http://php.net/manual/en/function.set-include-path.php

请参阅set_include_path()include_path ini设置的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM