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循环获取GET和POST值的代码

复制代码 代码如下:if(is_array($HTTP_GET_VARS))     {     &nbs...

PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】

本文实例讲述了PHP的图像处理。分享给大家供大家参考,具体如下:1、添加文字水印//1、打开图片资源   $src="./material/sea.jpg&...

PHP与SQL注入攻击[三]

这几天太忙,继续连载哈哈,争取半个月结束。 上文说到数据库自带的不安全输入过滤功能,但这样的功能不是所有数据库都有的。目前大概只有MySQL,SQLite,PostgreSQL,Syba...

利用PHP生成静态html页面的原理

利用PHP生成静态html页面的原理

前言 如果每次用户点击动态链接的时候都会对服务器发送数据查询的要求,对于一个访问量可能达百万千万级别的网站来说 这无疑是服务器一个大大的负担,所以把动态数据转换成静态html页面就成了节...

关于php 高并发解决的一点思路

我的思路如下(伪代码): sql1:查询商品库存 if(库存数量 > 0) { //生成订单... sql2:同时库存-1 } 当没有并发时,上面的流程看起来是再正常不过了...