调用未定义的函数apache_request_headers()

Rob*_*Rob 35 php

我刚刚将脚本切换到另一台服务器.在以前的服务器上,这完美无缺,现在我已经将它们切换到不同的服务器,我无法理解这个问题.

我不确定它会有所帮助,但这是相关的代码.

$headers = apache_request_headers();

PHP版本是:PHP 5.3.2

任何帮助,将不胜感激.

Bab*_*emi 56

您可以使用以下替换功能:

<?php
if( !function_exists('apache_request_headers') ) {
///
function apache_request_headers() {
  $arh = array();
  $rx_http = '/\AHTTP_/';
  foreach($_SERVER as $key => $val) {
    if( preg_match($rx_http, $key) ) {
      $arh_key = preg_replace($rx_http, '', $key);
      $rx_matches = array();
      // do some nasty string manipulations to restore the original letter case
      // this should work in most cases
      $rx_matches = explode('_', $arh_key);
      if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
        foreach($rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
        $arh_key = implode('-', $rx_matches);
      }
      $arh[$arh_key] = $val;
    }
  }
  return( $arh );
}
///
}
///
?>
Run Code Online (Sandbox Code Playgroud)

来源:PHP手册

  • 你需要生成第一个字符大写,改为:$ rx_matches [$ ak_key] = ucfirst($ ak_val); 要:$ rx_matches [$ ak_key] = ucfirst(strtolower($ ak_val)); (3认同)

cee*_*yoz 27

文档开始,在PHP 5.4.0发布之前:

仅当PHP作为Apache模块安装时,才支持此功能.

PHP 5.4.0及更高版本无条件地支持此功能.

所述文档还包括apache_request_headers通过单步模拟功能的替换功能$_SERVER.

  • [当前文档](http://www.php.net/manual/zh/intro.apache.php)仍然提到`apache _ *()`函数仅在将PHP作为Apache模块运行时才可用。[apache_request_headers()]的[特定文档](http://www.php.net/manual/zh/function.apache-request-headers.php)表示,它现在(&gt; = 5.4.0)在FastCGI下可用,但我怀疑它是否可以*(无条件)在IIS下使用? (3认同)
  • 我在 ubuntu 服务器的 Nginx 上运行 PHP 7.2.19-,我不认为“apache_request_headers()”方法无条件可用。我收到同样的错误“未捕获错误:调用未定义的函数 apache_request_headers()” (2认同)

Cha*_*dla 17

如果php作为Apache模块安装:

apache_request_headers()["Authorization"];
Run Code Online (Sandbox Code Playgroud)

否则,转到.htaccess文件并添加:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用以下任意方式访问请求标头:

$_SERVER["HTTP_AUTHORIZATION"]; // using super global
Run Code Online (Sandbox Code Playgroud)

要么

$app->request->headers("Authorization"); // if using PHP Slim
Run Code Online (Sandbox Code Playgroud)