调用未定义的函数apache_request_headers()
我刚将脚本切换到不同的服务器。在以前的服务器上,这个工作完美无瑕,现在我已经将它们切换到不同的服务器,我无法理解这个问题。调用未定义的函数apache_request_headers()
我不确定这会有帮助,但这里是相关的代码。
$headers = apache_request_headers();
PHP版本:PHP 5.3.2
任何帮助,将不胜感激。
从docs,PHP 5.4.0之前发布:
当PHP安装为Apache模块只支持此功能。
PHP 5.4.0及更高版本无条件支持此功能。
所述文档还包括通过步进$_SERVER
来模拟apache_request_headers
的功能的替换功能。
您可以使用下面的替换功能:
<?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
效果不错! – Zabs 2012-11-05 16:23:49
我在我的网站上使用过这个功能,但它似乎运行缓慢,有什么方法可以加速吗? – Zabs 2012-11-06 11:30:14
@Zabs:是的,你可以在这个函数中设置'$ arh'作为一个静态变量来确保它只运行一次。 [示例](http://pastebin.com/2NGARe49) – machineaddict 2013-10-02 09:20:23
如the other answer here的建议,我已经使用从PHP documentation注释的功能,却发现它是最理想的,硬阅读/维护,而不是完整的(与一些标题的(不符合)的外壳相比)。
,是因为我需要真的能够依靠它,我重新编码它是更加明显,处理更多的边缘的情况下更好地以及 - 原代码甚至指出“做一些讨厌的字符串操作来恢复原信件案例“和”这应该在大多数情况下工作“,这听起来不太好,你应该能够依靠。
这并不完美,但我发现它更可靠。它缺乏的一件事是处理实际的或原始的头文件,因为对输出结果将反映对$_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;
}
}
如果PHP安装为Apache模块:
apache_request_headers()["Authorization"];
否则,转到。htaccess的文件,并添加:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
然后,您可以访问使用任何这些请求头:
$_SERVER["HTTP_AUTHORIZATION"]; // using super global
OR
使用“apache_request_headers时$app->request->headers("Authorization"); // if using PHP Slim
这对我有用 – adrian4aes 2015-04-24 22:22:54
工作正常以获得授权,谢谢:) – Tiger 2016-03-17 05:22:29
需要.htaccess和@Babatunde Adeyemi的功能。谢谢! – 2016-11-16 23:02:19
同样的事情发生在我身上() “所以我用这个代码 - 完美地为我输出所有的标题:
<?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
如果使用此函数,我无法获得授权标头 – adrian4aes 2015-04-24 22:15:13
为什么在回显它之前将值赋给$ i [$ headers]? – 2018-01-19 12:21:41
它为我工作,谢谢..我赶上if($我[$头] ==“授权”){ – 2018-03-05 06:35:26
[当前文档](http://www.php.net/manual/en/intro.apache.php)仍然提到'阿帕奇_ *()'函数是仅在作为Apache模块运行PHP时可用。 'apache_request_headers()'的[specific doc](http://www.php.net/manual/en/function.apache-request-headers.php)表示现在(> = 5.4.0)在FastCGI下可用,但我怀疑它可以*无条件*,例如在IIS下? – Benjamin 2014-06-27 13:41:54