PHP獲取http請(qǐng)求的頭信息實(shí)現(xiàn)步驟
更新時(shí)間:2012年12月16日 14:36:01 作者:
PHP如何獲取http請(qǐng)求頭信息,是一個(gè)急切解決而不知道如何抉擇的問題,本人搜集整理下,可供參考下
PHP手冊(cè)提供了現(xiàn)成的函數(shù):
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";
}
?>
不過這個(gè)函數(shù)只能在apache環(huán)境下使用,iis或者nginx并不支持,可以通過自定義函數(shù)實(shí)現(xiàn)
<?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());
獲得結(jié)果:
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
)
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
復(fù)制代碼 代碼如下:
<?php
foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}
?>
不過這個(gè)函數(shù)只能在apache環(huán)境下使用,iis或者nginx并不支持,可以通過自定義函數(shù)實(shí)現(xiàn)
復(fù)制代碼 代碼如下:
<?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>
?>
好了,看看都打印出了啥吧
復(fù)制代碼 代碼如下:
<?php
print_r(getallheaders());
獲得結(jié)果:
復(fù)制代碼 代碼如下:
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之curl實(shí)現(xiàn)http與https請(qǐng)求的方法
- PHP實(shí)現(xiàn)取得HTTP請(qǐng)求的原文
- php中調(diào)用其他系統(tǒng)http接口的方法說明
- PHP 使用header函數(shù)設(shè)置HTTP頭的示例解析 表頭
- php抓取https的內(nèi)容的代碼
- php curl 獲取https請(qǐng)求的2種方法
- 在PHP中實(shí)現(xiàn)使用Guzzle執(zhí)行POST和GET請(qǐng)求
- 在Laravel中使用GuzzleHttp調(diào)用第三方服務(wù)的API接口代碼
- 使用Zttp簡(jiǎn)化Guzzle 調(diào)用
- PHP的HTTP客戶端Guzzle簡(jiǎn)單使用方法分析
相關(guān)文章
php關(guān)聯(lián)數(shù)組快速排序的方法
這篇文章主要介紹了php關(guān)聯(lián)數(shù)組快速排序的方法,涉及php數(shù)組排序的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
php實(shí)現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài)
這篇文章主要介紹了php實(shí)現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài),Varnish是一款高性能的開源HTTP加速器,可以替代Squid、Nginx等服務(wù)器,需要的朋友可以參考下2014-12-12
修改Zend引擎實(shí)現(xiàn)PHP源碼加密的原理及實(shí)踐
來源:phphot PHP文件的源碼都是明文,這對(duì)于某些商業(yè)用途來說,并不適合。 因此考慮使用加密的手段保護(hù)源碼。 實(shí)在不耐煩等待zend出編譯器,而且編譯和加密本質(zhì)上不是一回事兒。自己動(dòng)手、開始修改。2008-04-04
php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
這篇文章主要介紹了php的圖片處理實(shí)現(xiàn)代碼,包括縮放、剪裁、縮放、翻轉(zhuǎn)、旋轉(zhuǎn)、透明、銳化等圖片操作,需要的朋友可以參考下2013-07-07
php 的加密函數(shù) md5,crypt,base64_encode 等使用介紹
php 在做注冊(cè)、登錄或是url 傳遞參數(shù)時(shí)都會(huì)用到 字符變量的加密,下面我們就來簡(jiǎn)單的介紹下:php 自帶的加密函數(shù)2012-04-04

