PHP获取http头信息和CI中获取HTTP头信息的方法

简介: CI中获取HTTP头信息的方法: $this->input->request_headers() 在不支持apache_request_headers()的非Apache环境非常有用。返回请求头(header)数组。

CI中获取HTTP头信息的方法:

$this->input->request_headers()

在不支持apache_request_headers()的非Apache环境非常有用。返回请求头(header)数组。

$headers = $this->input->request_headers();

 

----------------------------------------------------------------------------------------------

获取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]  view plain copy
 
  1. <?php  
  2.   
  3. foreach (getallheaders() as $name => $value) {  
  4.     echo "$name: $value\n";  
  5. }  
  6.   
  7. ?>   

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

 

 

[php]  view plain copy
 
  1. <?php   
  2. <span class="html">if (!function_exists('getallheaders'))   
  3. {  
  4.     function getallheaders()   
  5.     {  
  6.        foreach ($_SERVER as $name => $value)   
  7.        {  
  8.            if (substr($name, 0, 5) == 'HTTP_')   
  9.            {  
  10.                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;  
  11.            }  
  12.        }  
  13.        return $headers;  
  14.     }  
  15. }</span>   
  16. ?>   


好了,看看都打印出了啥吧

 

 

[php]  view plain copy
 
  1. <?php  
  2. print_r(getallheaders());  


获得结果:

[html]  view plain copy
 
    1. Array  
    2. (  
    3.     [Accept] => */*  
    4.     [Accept-Language] => zh-cn  
    5.     [Accept-Encoding] => gzip, deflate  
    6.     [User-Agent] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727)  
    7.     [Host] => localhost  
    8.     [Connection] => Keep-Alive  
    9. )  
如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
目录
相关文章
|
16天前
|
缓存 PHP 开发者
PHP中的自动加载机制及其优化方法
传统的PHP开发中,经常会遇到类文件加载繁琐、效率低下的情况,而PHP的自动加载机制能够很好地解决这一问题。本文将深入探讨PHP中的自动加载机制,介绍其原理及实现方式,并提出了一些优化方法,帮助开发者提升代码加载效率,提高应用性能。
|
28天前
|
SQL 缓存 PHP
PHP技术探究:优化数据库查询效率的实用方法
本文将深入探讨PHP中优化数据库查询效率的实用方法,包括索引优化、SQL语句优化以及缓存机制的应用。通过合理的优化策略和技巧,可以显著提升系统性能,提高用户体验,是PHP开发者不容忽视的重要议题。
|
3月前
|
网络协议 网络架构
HTTP方法有哪些?
HTTP方法有哪些?
|
3月前
|
存储 iOS开发 开发者
使用克魔助手进行iOS数据抓包和HTTP抓包的方法详解
使用克魔助手进行iOS数据抓包和HTTP抓包的方法详解
46 0
|
4月前
|
JSON PHP 数据格式
PHP curl方法封装
PHP curl方法封装
31 0
|
2月前
|
Web App开发 存储 缓存
三、《图解HTTP》- 报文内的 HTTP信息
三、《图解HTTP》- 报文内的 HTTP信息
41 0
|
4月前
|
PHP
PHP显示报错提示,开启display_errors的方法
PHP显示报错提示,开启display_errors的方法
46 0