本文介绍了PHP:可以设置默认的php.ini文件常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个常数(字​​符串),可用于服务器上所有的PHP脚本。

I want to have a constant (a string) that is available to all PHP-scripts on the server.

根据这是,如果你解析一个额外的.ini文件很容易,但我不想来解析一个额外的文件,我想设置我不断在全球的php.ini而不必解析脚本中任何东西。 (其实这是整点,因为我需要不断的寻找的东西,包括/分析/ etc:当我知道这个额外的.ini文件会是这样,我不需要它了)

According to http://php.net/manual/en/function.parse-ini-file.php this is quite easy if you parse an extra .ini file, however I don't want to parse an extra file, I want to set my constant in the global php.ini without having to parse anything in the scripts. (In fact that's the whole point because I need the constant to find the stuff to include/parse/etc: When I know where this extra .ini file would be, I don't need it anymore!)

刚刚发明在php.ini一个新的常数,然后试图用ini_get()不工作来访问它,有没有其他办法?

Just inventing a new constant in php.ini and then trying to access it with ini_get() does not work, is there any other way?

我编译Apache和PHP自己,所以我也可以设定参数在编译时和/或使用Apache的常量如果时,只需要

I compile Apache and PHP myself, so I could also set the constant at compile-time and/or use Apache-constants if that is neccessary.

推荐答案

您可以使用PHP <一个href=\"http://www.php.net/manual/en/ini.core.php#ini.auto-$p$ppend-file\"><$c$c>auto_$p$ppend_file在你的PHP INI做这个脚本,因为它会之前的任何用户,土地脚本运行:

You can use a PHP auto_prepend_file script in your PHP ini to do this as it will be run before any of your user-land scripts:

指定之前自动解析的文件名
  主文件。该文件包括,如果它被称为与需要
  功能,所以include_path中被使用。

所以,你可以添加一个ini一行:

So you can add an ini line like:

auto_prepend_file="/home/user/script.php"

中/home/user/script.php:

The in /home/user/script.php:

define('CONSTANT_NAME', 'your nice value here');

现在在你的PHP脚本,您可以访问 CONSTANT_NAME 从你喜欢的地方,因为它是在所有的PHP脚本可用。

Now in your PHP scripts you can access CONSTANT_NAME from wherever you like as it is available in all PHP scripts.

我用这个技术,它使用了mod_rewrite群众基​​础的虚拟主机,所以我可以给我的PHP脚本准确的文档根目录我的临时服务器上。我已经在blog帖子之前。

I use this technique on my staging server that uses mod_rewrite based mass virtual hosting so I can give my PHP scripts an accurate document root. I have discussed this in a blog post before.

这篇关于PHP:可以设置默认的php.ini文件常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 03:00