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

8xiog9wr  于 5个月前  发布在  PHP
关注(0)|答案(7)|浏览(36)

我刚刚把我的脚本切换到另一个服务器上。在以前的服务器上,这是正常工作的,现在我把它们切换到另一个服务器上,我不明白这个问题。
我不确定这是否有用,但这是相关代码。
第一个月
PHP版本:PHP 5.3.2

lf5gs5x2

lf5gs5x21#

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

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

nuypyhwy

nuypyhwy2#

docs开始,在PHP 5.4.0发布之前:
只有当PHP作为Apache模块安装时才支持此功能。
PHP 5.4.0及更高版本无条件支持此功能。
上述文档还包括通过逐步遍历$_SERVER来模拟apache_request_headers功能的替换函数。

vjrehmav

vjrehmav3#

如果php安装为Apache模块:

apache_request_headers()["Authorization"];

字符串
否则,转到**.htaccess**文件并添加:

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


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

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


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

sauutmhj

sauutmhj4#

同样的事情发生在我使用“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

guicsvcw

guicsvcw5#

正如the other answer here中所建议的,我使用了PHP文档中注解中的函数,但发现它不是最佳的,难以阅读/维护,并且与某些头文件的(不符合) shell 相比不完整。
因此,因为我需要 * 真的 * 能够依赖它,我重新编码了它,使其更明显,并更好地处理更多的边缘情况-原始代码甚至声明 “做一些讨厌的字符串操作来恢复原始字母大小写”“这应该在大多数情况下工作”,这听起来不太好,因为你应该能够依赖它。
它并不完美,但我发现它更可靠。它缺少的一件事是处理实际或原始的头部,因为对$_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;

    }
}

字符串

wmomyfyw

wmomyfyw6#

您可以使用以下函数在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

jucafojl

jucafojl7#

如果apache_request_headers函数在您的PHP环境中不可用,您可以使用以下替换函数来获取HTTP头:

if (!function_exists('apache_request_headers')) {
    function apache_request_headers() {
        $headers = [];

        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $formatted_name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
                $headers[$formatted_name] = $value;
            }
        }

        return $headers;
    }
}

字符串
此函数遍历$_SERVER数组,提取HTTP标头,并将其名称格式化为遵循常规的标题大小写和连字符。它可以用作以一致方式检索HTTP标头的回退。

相关问题