php 调用未定义的函数 apache_request_headers()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2916232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:08:10  来源:igfitidea点击:

Call to undefined function apache_request_headers()

php

提问by Rob

I've just switched my scripts to a different server. On the previous server this worked flawlessly, and now that I've switched them to a different server, I can't understand the problem.

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

I'm not sure it would help, but here's the relevant code.

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

$headers = apache_request_headers();

$headers = apache_request_headers();

PHP Version is: PHP 5.3.2

PHP 版本为:PHP 5.3.2

采纳答案by ceejayoz

From the docs, before the release of PHP 5.4.0:

来自docs,在 PHP 5.4.0 发布之前:

This function is only supported when PHP is installed as an Apache module.

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

PHP 5.4.0 and later support this function unconditionally.

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

Said docs also include replacement functions that mimic the functionality of apache_request_headersby stepping through $_SERVER.

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

回答by Babatunde Adeyemi

You can use the following replacement function:

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

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

Source: PHP Manual

来源:PHP 手册

回答by Chanakya Vadla

if php is installed as an Apache module:

如果 php 安装为Apache 模块

apache_request_headers()["Authorization"];

else, go to .htaccessfile and add:

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

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=

You can then access request headers using any of these:

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

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

OR

或者

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

回答by sputn1k

same thing happened to me when using "apache_request_headers()" so i used this code - works perfectly for me to output all the headers:

使用“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>";
        }
    }

?>

output example:

输出示例:

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

回答by Flygenring

As suggested in the other answer here, I have used the function from the comments in the PHP documentation, but found that it's suboptimal, hard to read/maintain, and not complete compared to the (non-conforming) casing of some headers.

正如此处的另一个答案所建议的那样,我使用了PHP 文档中注释中的函数,但发现与某些标题的(不符合标准的)大小写相比,它不是最佳的,难以阅读/维护,并且不完整。

So because I needed to reallybe able to rely on it, I recoded it to be more obvious and handle more edge-cases better as well – the original code even states "do some nasty string manipulations to restore the original letter case"and "this should work in most cases", which doesn't sound nice for something you should be able to depend on.

所以,因为我需要真正能够依靠它,我重新编码它是更加明显,处理更多的边缘的情况下更好地以及-原代码甚至指出“做一些讨厌的字符串操作,以恢复原来的字母大小写”“这应该在大多数情况下工作",这对于您应该能够依赖的东西来说听起来不太好。

It's not perfect, but I find that it's more reliable. One thing it lacks is to work on the actual or original headers, as any modifications to $_SERVERwill be reflected in the output. This can be mitigated by making the result static and running the function as the first thing on every request.

它并不完美,但我发现它更可靠。它缺少的一件事是处理实际或原始标题,因为任何修改$_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;

    }
}

回答by Sajith Dushmantha Samarathunga

Pleae use follownig methord to get headers using PHP in Nginx

请使用 follownig 方法在 Nginx 中使用 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

来自此页面的来源