无法在 PHP 中检索 Apache 环境变量
背景
我有一个 Apache/2.2.15 (Win32),设置了 PHP/5.3.2,处理身份验证。
<Directory /usr/www/myhost/private>
# core authentication and mod_auth_basic configuration
# for mod_authn_dbd
AuthType Basic
AuthName "My Server"
AuthBasicProvider dbd
# core authorization configuration
Require valid-user
# mod_authn_dbd SQL query to authenticate a user
AuthDBDUserPWQuery "SELECT Password,UserName,Realm,Access FROM authn WHERE user = %s"
</Directory>
认证工作正常!没问题。
但是对于 文档,从 AuthDBDUserPWQuery 返回的任何额外字段都会放入环境中的 AUTHENTICATION_fieldname 变量中。
使用 phpinfo(),我可以在“Apache 环境”下看到这些变量及其正确的值。
AUTHENTICATE_USERNAME
AUTHENTICATE_REALM
AUTHENTICATE_ACCESS
问题
我无法从我的 php.ini 中获取这些环境变量。
1 <?php
2 $Access = apache_getenv('AUTHENTICATE_ACCESS',true);
3 var_dump($Access);
4 ?>
第 3 行打印 bool(false) 表明未找到该变量!
但是,如果我更改为另一个 Apache 环境变量,例如“HTTP_HOST”,它就可以工作。
..是的,我也尝试过 getenv() ,结果相同。
还有需要注意的是,Apache服务器需要使用APR 1.3.0编译才能工作。我使用了来自 httpd.apache.org 的 Apache msi 版本,它似乎是使用高于版本 2 的 APR 进行编译的。因为我可以使用 phpinfo()
看到它们,所以它们必须可以从 PHP 访问。
Background
I have an Apache/2.2.15 (Win32) with PHP/5.3.2 set up, handling authentication.
<Directory /usr/www/myhost/private>
# core authentication and mod_auth_basic configuration
# for mod_authn_dbd
AuthType Basic
AuthName "My Server"
AuthBasicProvider dbd
# core authorization configuration
Require valid-user
# mod_authn_dbd SQL query to authenticate a user
AuthDBDUserPWQuery "SELECT Password,UserName,Realm,Access FROM authn WHERE user = %s"
</Directory>
The authentication works fine! No problems.
But regarding to the documentation, any extra field returned back from the AuthDBDUserPWQuery will be put into an AUTHENTICATION_fieldname variable in the Environment.
With phpinfo()
, I can see these variables with he correct values under "Apache Environment".
AUTHENTICATE_USERNAME
AUTHENTICATE_REALM
AUTHENTICATE_ACCESS
Problem
I can't fetch those environment variables from my php.
1 <?php
2 $Access = apache_getenv('AUTHENTICATE_ACCESS',true);
3 var_dump($Access);
4 ?>
Line 3 prints bool(false) indicating that the variable wasn't found!
However if I change to another Apache Environment variable such as 'HTTP_HOST' it works.
..and yes, I have tried getenv()
also, same result.
There is also a note that the Apache server needs to be compiled with APR 1.3.0 to work. I used the Apache msi build from httpd.apache.org and it seems to be compiled with APR above version 2. Since I can see them with phpinfo()
they must be accessible from PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已在 .htaccess 文件中使用此 mod_rewrite 规则,以使 HTTP Authorization 标头环境变量在
$_SERVER['HTTP_AUTHORIZATION']
中可用。我确信这可以适合您的目的。不确定这是否是最好的解决方案,但它是一个解决方案:I have used this mod_rewrite rule in an .htaccess file to make the HTTP Authorization header environment variable available in
$_SERVER['HTTP_AUTHORIZATION']
. I'm sure this could be adapted for your purposes. Not sure if it's the best solution, but it's a solution:我做了一个解决方案,
看来这可能是一个 PHP 错误。发现 PHP 4 报告的一些相关错误,也许他们还没有修复它们...
我做了一个我真的不喜欢的解决方案(因为我正在访问 Apache userdata 表),但似乎我没有选择。
如果 PHP 未能正确更新 $_ENV,这将从 Apache 用于身份验证的同一数据库和表中检索当前登录的用户。
然后额外的字段将被写入全局 $_ENV 变量中,以便可以按预期使用。
稍后当“bug”修复后,它将自动使用原来的$_ENV。
如果有人可以提供有关此主题的一些最新信息,我会很高兴......
I made a work around,
It seems like this might be a PHP bug. Found some related bugs reported for PHP 4 and maybe they haven't fixed them yet...
I did a solution that I really don't like (because I'm accessing the Apache userdata table), but it seems that I have no choice.
If PHP has failed to update $_ENV correct, this will retrieve the current logged in user from the same database and table that Apache is using for authentication.
Then the extra fields will be written into the global $_ENV variable so it can be used as it is supposed.
Later when the "bug" is fixed, it will automatically use the original $_ENV.
If anyone can bring some up to date information on this topic, I would be happy...
deceze的回答里有一条线索。他从 $_SERVER 而不是 $_ENV 获取数据。
我在主 httpd.conf 中使用 SenEnv MY_VAR“true” 设置 Apache Env Var
,但在 $_ENV 中看不到它。但它在 $_SERVER 中。
There is a clue in deceze's answer. He is fetching the data from $_SERVER instead of $_ENV.
I was setting a Apache Env Var with
SenEnv MY_VAR "true" in the main httpd.conf and couldn't see it in $_ENV. It was in $_SERVER though.
最近我写了一个库来从环境变量中获取值并解析为 PHP 数据类型。该库可用于将环境变量解析为 PHP 数据类型(例如转换为整数、浮点数、空值、布尔值),解析复杂的数据结构(例如 JSON 字符串)以及社区的贡献。
该库位于此处: https://github.com/jpcercal/environment
使用 . htaccess 示例:
并从环境变量(独立于环境 CLI、Apache、Nginx、PHP 内置服务器等)获取值来执行此操作:
享受它。
Recently i wrote a library to get values from environment variables and parse to the PHP data types. This library can be used to parse environment variables to PHP data types (like the casting to integer, float, null, boolean), parse the complex data structures like a JSON string and more with the contribution of the commnunity.
The library is available here: https://github.com/jpcercal/environment
Setup your environment variables with .htaccess by example:
And to get the values from environment variable (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:
Enjoy it.