简体   繁体   English

php,apache&.htaccess:全局包含php配置文件

[英]php, apache & .htaccess: include php configuration file globally

i have a php project which has a configuration file config.php that stores config information like database info, directories, etc. 我有一个php项目,它有一个配置文件config.php,用于存储配置信息,如数据库信息,目录等。

normally we would include this config file at the top of every php page like require_once(".../config.php); however, i have found a way to do this by creating a .htaccess file and include this: 通常我们会在每个php页面的顶部包含这个配置文件,如require_once(“... / config.php);但是,我已经找到了一种方法来创建一个.htaccess文件并包含这个:

php_value  auto_prepend_file  /home/public_html/..../config.php

this works fine until i transferred it to the shared hosting. 这工作正常,直到我将其转移到共享主机。 apparently, this shared hosting allows us to change the PHP version from 4x to 5x. 显然,这个共享托管允许我们将PHP版本从4x更改为5x。 After changing to 5x, i realised they have created a .htaccess file with the following content: 更改为5x后,我意识到他们已经创建了一个.htaccess文件,其中包含以下内容:

<FilesMatch "\.php$"> # phpvs v5
AddHandler x-httpd-php5 .php # phpvs v5
</FilesMatch> # phpvs v5

with this, it ignores the include path to the config file.. how can i go about fixing this? 有了这个,它忽略了配置文件的包含路径..我该如何解决这个问题呢? i certainly do not wanna include my config.php in every php files. 我当然不想在每个php文件中包含我的config.php。

An htaccess file is not restricted to containing a single directive. htaccess文件不限于包含单个指令。 You can have both. 你可以两个都有。

normally we would include this config file at the top of every php page like require_once(".../config.php); however, i have found a way to do this by creating a .htaccess file and include this 通常我们会在每个php页面的顶部包含这个配置文件,例如require_once(“... / config.php);但是,我已经找到了一种方法来创建一个.htaccess文件并包含这个

Probably not the wisest move. 可能不是最明智的举动。 It is more stable and portable to do this in your PHP than to rely on .htaccess. 在PHP中执行此操作比使用.htaccess更稳定和可移植。 Relying on .htaccess means if your web server is changed or reconfigured it's likely to stop working (as you found). 依赖.htaccess意味着如果您的Web服务器被更改或重新配置,它可能会停止工作(如您所见)。

I know it doesn't answer your question but I would consider going back to doing this using require_once() in the appropriate PHP files. 我知道它没有回答你的问题,但我会考虑在相应的PHP文件中使用require_once()回去做这件事。

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

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