PHP获取http请求的头信息实现步骤

yipeiwu_com6年前PHP代码库
PHP手册提供了现成的函数:
getallheaders
(PHP 4, PHP 5)
getallheaders — Fetch all HTTP request headers
说明
array getallheaders ( void )
Fetches all HTTP headers from the current request.
This function is an alias for apache_request_headers(). Please read theapache_request_headers() documentation for more information on how this function works.
返回值
An associative array of all the HTTP headers in the current request, orFALSE on failure.
Example #1 getallheaders() example
复制代码 代码如下:

<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
?>

不过这个函数只能在apache环境下使用,iis或者nginx并不支持,可以通过自定义函数实现
复制代码 代码如下:

<?php
<SPAN class=html>if (!function_exists('getallheaders'))
{
    function getallheaders()
    {
       foreach ($_SERVER as $name => $value)
       {
           if (substr($name, 0, 5) == 'HTTP_')
           {
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
           }
       }
       return $headers;
    }
}</SPAN>
?>

好了,看看都打印出了啥吧
复制代码 代码如下:

<?php
print_r(getallheaders());

获得结果:
复制代码 代码如下:

Array
(
[Accept] => */*
[Accept-Language] => zh-cn
[Accept-Encoding] => gzip, deflate
[User-Agent] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)
[Host] => localhost
[Connection] => Keep-Alive
)

相关文章

PHP文件操作实例总结

PHP文件操作实例总结

本文实例总结了PHP文件操作。分享给大家供大家参考,具体如下: 操作文件的常用方法: flie_put_contents(url,str); file_get_contents(url...

php实现的mongodb操作类实例

本文实例讲述了php实现的mongodb操作类。分享给大家供大家参考。具体如下: <?php /* * To change this template, choo...

PHP实现二维数组中的查找算法小结

本文实例讲述了PHP实现二维数组中的查找算法。分享给大家供大家参考,具体如下: 方法1:silu从左下角最后一行的第一个元素开始,遍历。如果小于target 则遍历该行的所有元素,找到结...

深入解析PHP垃圾回收机制对内存泄露的处理

上次说到了refcount和is_ref,这里来说说内存泄露的情况复制代码 代码如下:$a = array(1, 2, &$a);unset($a);在老的PHP版本中,这里就会出现内存...

php优化及高效提速问题的实现方法第1/2页

一、 在函数中,传递数组时使用 return 比使用 global 要高效,比如: function userloginfo($usertemp){ $detail=explode("|...