调用未定义的函数 apache_request_headers()

php
2022-08-30 11:36:18

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

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

$headers = apache_request_headers();

PHP 版本是: PHP 5.3.2


答案 1

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

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


答案 2

在 PHP 5.4.0 发布之前,从文档中可以看出:

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

PHP 5.4.0 及更高版本无条件地支持此函数。

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


推荐