无法在 PHP 中检索 Apache 环境变量

发布于 2024-10-05 10:31:53 字数 1307 浏览 7 评论 0原文

背景

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剪不断理还乱 2024-10-12 10:31:53

我已在 .htaccess 文件中使用此 mod_rewrite 规则,以使 HTTP Authorization 标头环境变量在 $_SERVER['HTTP_AUTHORIZATION'] 中可用。我确信这可以适合您的目的。不确定这是否是最好的解决方案,但它是一个解决方案:

RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]

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:

RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
破晓 2024-10-12 10:31:53

我做了一个解决方案,

看来这可能是一个 PHP 错误。发现 PHP 4 报告的一些相关错误,也许他们还没有修复它们...

我做了一个我真的不喜欢的解决方案(因为我正在访问 Apache userdata 表),但似乎我没有选择。

//************************************************************* 
// If PHP failed to retrieve the AuthDBDUserPWQuery fields.
// Connect to Apache authentication databaseand create the
// envirnment variables manually
//
if (empty($_ENV['AUTHENTICATE_ACCESS'])) {
  $Apache = mysql_connect('MyServerIP','MyUserName','MyPassword',false,MYSQL_CLIENT_SSL|MYSQL_CLIENT_COMPRESS);
  mysql_select_db('MyDatabase',$Apache);
  $SQLSet = mysql_query("SELECT Realm, Access FROM authenticationtable WHERE UserName='".$_SERVER['PHP_AUTH_USER']."' AND Password='".$_SERVER['PHP_AUTH_PW']."'");
  $SQLRow = mysql_fetch_array($SQLSet);
  $_ENV['AUTHENTICATE_REALM'] = $SQLRow['Realm'];
  $_ENV['AUTHENTICATE_ACCESS']= $SQLRow['Access'];
  mysql_close($Apache);
}

如果 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 failed to retrieve the AuthDBDUserPWQuery fields.
// Connect to Apache authentication databaseand create the
// envirnment variables manually
//
if (empty($_ENV['AUTHENTICATE_ACCESS'])) {
  $Apache = mysql_connect('MyServerIP','MyUserName','MyPassword',false,MYSQL_CLIENT_SSL|MYSQL_CLIENT_COMPRESS);
  mysql_select_db('MyDatabase',$Apache);
  $SQLSet = mysql_query("SELECT Realm, Access FROM authenticationtable WHERE UserName='".$_SERVER['PHP_AUTH_USER']."' AND Password='".$_SERVER['PHP_AUTH_PW']."'");
  $SQLRow = mysql_fetch_array($SQLSet);
  $_ENV['AUTHENTICATE_REALM'] = $SQLRow['Realm'];
  $_ENV['AUTHENTICATE_ACCESS']= $SQLRow['Access'];
  mysql_close($Apache);
}

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...

绝不放开 2024-10-12 10:31:53

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.

温柔戏命师 2024-10-12 10:31:53

最近我写了一个库来从环境变量中获取值并解析为 PHP 数据类型。该库可用于将环境变量解析为 PHP 数据类型(例如转换为整数、浮点数、空值、布尔值),解析复杂的数据结构(例如 JSON 字符串)以及社区的贡献。

该库位于此处: https://github.com/jpcercal/environment

使用 . htaccess 示例:

SetEnv YOUR_ENV_VARIABLE_NAME the-value-of-your-env-var

并从环境变量(独立于环境 CLI、Apache、Nginx、PHP 内置服务器等)获取值来执行此操作:

<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));

享受它。

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:

SetEnv YOUR_ENV_VARIABLE_NAME the-value-of-your-env-var

And to get the values from environment variable (independently of the environment CLI, Apache, Nginx, PHP Built-in Server and more) to do it:

<?php
// ...
require "vendor/autoload.php";
// ...
var_dump(Cekurte\Environment\Environment::get("YOUR_ENV_VARIABLE_NAME"));

Enjoy it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文