调用未定义的函数apache_request_headers()

时间:2010-05-26 19:47:42

标签: php

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

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

$headers = apache_request_headers();

PHP版本是:PHP 5.3.2

任何帮助都将不胜感激。

6 个答案:

答案 0 :(得分:54)

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

<?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 );
}
///
}
///
?>

来源:PHP Manual

答案 1 :(得分:27)

在PHP 5.4.0发布之前的docs

  

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

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

所述文档还包括通过单步执行apache_request_headers来模仿$_SERVER功能的替换函数。

答案 2 :(得分:17)

如果php安装为 Apache模块

apache_request_headers()["Authorization"];

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

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

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

$_SERVER["HTTP_AUTHORIZATION"]; // using super global

OR

$app->request->headers("Authorization"); // if using PHP Slim

答案 3 :(得分:3)

根据the other answer here的建议,我使用了PHP documentation中的评论功能,但发现它不是最理想的,难以阅读/维护,并且与某些标题的(不符合)套管。

因为我需要真的能够依赖它,我将它重新编码为更明显并且更好地处理更多边缘情况 - 原始代码甚至声明&# 34;做一些令人讨厌的字符串操作来恢复原始字母案例&#34; &#34;这应该适用于大多数情况下&#34; ,这听起来并不好听对于你应该能够依赖的东西。

它并不完美,但我发现它更可靠。缺少的一件事是处理实际或原始标题,因为对$_SERVER的任何修改都将反映在输出中。这可以通过使结果静态并将函数作为每个请求的第一件事来缓解。

<?php
// Drop-in replacement for apache_request_headers() when it's not available
if(!function_exists('apache_request_headers')) {
    function apache_request_headers() {

        // Based on: http://www.iana.org/assignments/message-headers/message-headers.xml#perm-headers
        $arrCasedHeaders = array(
            // HTTP
            'Dasl'             => 'DASL',
            'Dav'              => 'DAV',
            'Etag'             => 'ETag',
            'Mime-Version'     => 'MIME-Version',
            'Slug'             => 'SLUG',
            'Te'               => 'TE',
            'Www-Authenticate' => 'WWW-Authenticate',
            // MIME
            'Content-Md5'      => 'Content-MD5',
            'Content-Id'       => 'Content-ID',
            'Content-Features' => 'Content-features',
        );
        $arrHttpHeaders = array();

        foreach($_SERVER as $strKey => $mixValue) {
            if('HTTP_' !== substr($strKey, 0, 5)) {
                continue;
            }

            $strHeaderKey = strtolower(substr($strKey, 5));

            if(0 < substr_count($strHeaderKey, '_')) {
                $arrHeaderKey = explode('_', $strHeaderKey);
                $arrHeaderKey = array_map('ucfirst', $arrHeaderKey);
                $strHeaderKey = implode('-', $arrHeaderKey);
            }
            else {
                $strHeaderKey = ucfirst($strHeaderKey);
            }

            if(array_key_exists($strHeaderKey, $arrCasedHeaders)) {
                $strHeaderKey = $arrCasedHeaders[$strHeaderKey];
            }

            $arrHttpHeaders[$strHeaderKey] = $mixValue;
        }

        return $arrHttpHeaders;

    }
}

答案 4 :(得分:3)

当我使用“apache_request_headers()”时发生了同样的事情,所以我使用了这段代码 - 完全适合我输出所有标题:

<?php

    $headers = array();

    foreach($_SERVER as $key => $value) {
        if(strpos($key, 'HTTP_') === 0) {
            $headers = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
            echo $headers." : ". $i[$headers] = $value . "<br>";
        }
    }

?>

输出示例:

Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding : gzip, deflate
Accept-Language : en-US,en;q=0.5
Cache-Control : max-age=0
Connection : keep-alive
Host : example.com
Referer : https://example.com/
User-Agent : Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0

答案 5 :(得分:0)

请在Nginx中使用follownig methord在PHP中获取标头

function getHeaders()
{
$headers = array();
foreach ($_SERVER as $k => $v)
{
if (substr($k, 0, 5) == "HTTP_")
{
$k = str_replace('_', ' ', substr($k, 5));
$k = str_replace(' ', '-', ucwords(strtolower($k)));
$headers[$k] = $v;
}
}
return $headers;
}

Source from this page