使用 php.ini 声明全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5052558/
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
Declaring global variable with php.ini
提问by Zstream
Is it possible to keep variables in php.ini file. Like that we do with the web.config in .net. I like to keep a flag type variable in the php.ini and use it to different projects.
是否可以在 php.ini 文件中保留变量。就像我们对 .net 中的 web.config 所做的那样。我喜欢在 php.ini 中保留一个标志类型变量并将其用于不同的项目。
回答by Alister Bulman
It's not possible to set user-level variables within a plain php.ini file (or the .htaccess equivilents). There are some PECL modules that do allow that, such as hidef(http://pecl.php.net/package/hidef) - though these would need to be installed on every installation you use.
无法在普通的 php.ini 文件(或 .htaccess 等效文件)中设置用户级变量。有一些 PECL 模块确实允许这样做,例如hidef(http://pecl.php.net/package/hidef)——尽管这些模块需要在您使用的每个安装中安装。
Including (or pre-including) a file with auto_prepend_fileis quite possible - though that would be on every PHP request.
包含(或预先包含)一个带有auto_prepend_file的文件是很有可能的——尽管这会出现在每个 PHP 请求中。
What is frequently done is setting an environment variable as part of the webserver process, which can be read from PHP. In Apache this is quite easy, with the SetEnvmodule.
经常做的是设置一个环境变量作为 web 服务器进程的一部分,它可以从 PHP 读取。在 Apache 中,这很容易,使用SetEnv模块。
SetEnv PRODUCTION_SERVER 1
And accessing it in PHP:
并在 PHP 中访问它:
if ($_ENV['PRODUCTION_SERVER']) {...} // or getenv('PRODUCTION_SERVER')
回答by Ascherer
Have you looked at get_cfg_var()
?
你看了get_cfg_var()
吗?
I needed to do something similar, and this was able to do it for me.
我需要做一些类似的事情,这对我来说能够做到。
回答by John Parker
Nope.
不。
You could use the auto_prepend_filedirective to automatically include a file that said, although as it uses the include_path, you'd need to specify the full path.
您可以使用auto_prepend_file指令自动包含一个文件,尽管它使用 include_path,但您需要指定完整路径。
However, it's probably more transparent just to explicitly include/require the relevant file.
但是,仅显式包含/要求相关文件可能更透明。
回答by Craig Jacobs
The accepted answere also worked for me, with one change.
接受的 answere 也对我有用,只有一个变化。
I didn't test this on earlier versions, but in my environment (php 5.4.22) this doesn't show up in $_ENV, but rather in $_SERVER.
我没有在早期版本上测试过这个,但在我的环境(php 5.4.22)中,这不会出现在 $_ENV 中,而是出现在 $_SERVER 中。
In my .htacess file:
在我的 .htacess 文件中:
SetEnv PRODUCTION_SERVER 0.
My php code:
我的php代码:
$production="PRODUCTION";
if (!isset($_SERVER['PRODUCTION_SERVER']) || $_SERVER['PRODUCTION_SERVER'] != 1){
$production="DEVELOPMENT";
}
回答by hendepher
One technique that I have found useful for passing a limited number of global variables to a bootstrap script is to take advantage of the SetEnv directive in an .htaccess file. The advantage is that the variable you set will be made available to any script in that directory, plus any scripts in child directories under it.
我发现将有限数量的全局变量传递给引导脚本的一种技术是利用 .htaccess 文件中的 SetEnv 指令。优点是您设置的变量将可用于该目录中的任何脚本,以及该目录下子目录中的任何脚本。
You could use a SetEnv varibale with the location of a configuration file, such as:
您可以将 SetEnv 变量与配置文件的位置一起使用,例如:
in .htaccess:
在 .htaccess 中:
SetEnv init_path /home/hendepher/TestApp/init/init.php
In your .php scipt:
在您的 .php 脚本中:
<?php
if(!getenv('init_path')) throw new Exception('Must set init_path in .htaccess');
require_once getenv('init_path');
.
.
.
?>
If you have a test directory that requires different initialization o global variables, simply add another .htaccess file in your test directory:
如果您的测试目录需要不同的初始化 o 全局变量,只需在您的测试目录中添加另一个 .htaccess 文件:
SetEnv init_path /home/hendepher/TestApp/init/testing_init.php
Doing it this way, as opposed to using the 'auto_prepend_file' directive, is that your global configuration script is not run by all the php applications on your server: some may not need it.
这样做,与使用 'auto_prepend_file' 指令相反,您的全局配置脚本不是由您服务器上的所有 php 应用程序运行的:有些可能不需要它。
回答by drewish
I don't think that's a good place to store variables. php.ini is for storing configuration for PHP itself not your applications. You should consider putting the shared variables into a .inc file and including that instead.
我认为这不是存储变量的好地方。php.ini 用于存储 PHP 本身的配置,而不是您的应用程序。您应该考虑将共享变量放入 .inc 文件中并改为包含该文件。