php设置文件路径的常量方法_PHP:可以设置默认的php.ini文件常量

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

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

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?

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.

解决方案

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:

Specifies the name of a file that is automatically parsed before the

main file. The file is included as if it was called with the require

function, so include_path is used.

So you can add an ini line like:

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

The in /home/user/script.php:

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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值