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

 更新时间:2012年12月16日 14:36:01   作者:  
PHP如何获取http请求头信息,是一个急切解决而不知道如何抉择的问题,本人搜集整理下,可供参考下
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'))
{
&nbsp;&nbsp;&nbsp; function getallheaders()
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp; &nbsp; &nbsp; foreach ($_SERVER as $name => $value)
&nbsp;&nbsp; &nbsp; &nbsp; {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (substr($name, 0, 5) == 'HTTP_')
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; return $headers;
&nbsp;&nbsp;&nbsp; }
}</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的mb扩展,教你如何使用原生态的php函数轻松处理字符串。
    2014-05-05
  • PHP设计模式之迭代器模式的深入解析

    PHP设计模式之迭代器模式的深入解析

    本篇文章是对PHP设计模式中的迭代器模式进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • PHPExcel内存泄漏问题解决方法

    PHPExcel内存泄漏问题解决方法

    这篇文章主要介绍了PHPExcel内存泄漏问题解决方法,本文先是讲解了造成内存泄漏的原因,然后给出了解决方法,需要的朋友可以参考下
    2015-01-01
  • php关联数组快速排序的方法

    php关联数组快速排序的方法

    这篇文章主要介绍了php关联数组快速排序的方法,涉及php数组排序的相关技巧,非常具有实用价值,需要的朋友可以参考下
    2015-04-04
  • php实现监控varnish缓存服务器的状态

    php实现监控varnish缓存服务器的状态

    这篇文章主要介绍了php实现监控varnish缓存服务器的状态,Varnish是一款高性能的开源HTTP加速器,可以替代Squid、Nginx等服务器,需要的朋友可以参考下
    2014-12-12
  • 修改Zend引擎实现PHP源码加密的原理及实践

    修改Zend引擎实现PHP源码加密的原理及实践

    来源:phphot PHP文件的源码都是明文,这对于某些商业用途来说,并不适合。 因此考虑使用加密的手段保护源码。 实在不耐烦等待zend出编译器,而且编译和加密本质上不是一回事儿。自己动手、开始修改。
    2008-04-04
  • php对图像的各种处理函数代码小结

    php对图像的各种处理函数代码小结

    这篇文章主要介绍了php的图片处理实现代码,包括缩放、剪裁、缩放、翻转、旋转、透明、锐化等图片操作,需要的朋友可以参考下
    2013-07-07
  • php 的加密函数 md5,crypt,base64_encode 等使用介绍

    php 的加密函数 md5,crypt,base64_encode 等使用介绍

    php 在做注册、登录或是url 传递参数时都会用到 字符变量的加密,下面我们就来简单的介绍下:php 自带的加密函数
    2012-04-04
  • php 分库分表hash算法

    php 分库分表hash算法

    分享一个分库分表hash算法,需要的朋友可以参考下。
    2009-11-11
  • 浅谈php处理后端&接口访问超时的解决方法

    浅谈php处理后端&接口访问超时的解决方法

    下面小编就为大家带来一篇浅谈php处理后端&接口访问超时的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-10-10

最新评论