下面为大家分享一篇php获取ajax的headers方法与内容实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
1.前端页面
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<title>cross-browser qrcode generator for javascript</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="qrcode.js"></script>
</head>
<body>
<button class="suning">点击</button>
<script type="text/javascript">
$(".suning").click(function(){
$.ajax({
url: 'headers.php',
data: {'key':'123'},//如果需要字符串json方式,请使用json.stringify(setting.data)
type: 'post',
//datatype: 'json',
//contenttype: 'application/json',
//processdata: true,//为true不会序列化数据
beforesend: function () {
// layer.load(2);
},
headers: {
"token":'token7758521'//自定义token及值
},
success: function (responsedata) {
console.log(responsedata);
},
error: function (error) {
console.log(error);
}
});
});
</script>
</body>
2.php页面
<?php
$arr = get_getallheaders();//获取http头数组
//echo $arr["token"];//输出token
var_dump($arr);//输出整个数组
function get_getallheaders() //定义方法
{
foreach ($_server as $name => $value) //循环_server数组
{
if (substr($name, 0, 5) == 'http_') //前5个字符是http_的进入循环
{
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
//注释
//substr($name, 5),从$name第5个字符向后截取
//str_replace('_', ' ',)下划线替换成空格
//strtolower()全部转换为小写
//ucwords()首字母转换为大写
//str_replace(' ', '-',)所有空格替换为-
}
}
return $headers; //返回前key前5个字符是http_的数组
//return $_server; //返回_server数组
}
3.php页面返回的headers
array(11) {
["cookie"]=>
string(94) "hm_lvt_f62fa14829605f0d29c05da9c30e045a=1503649309,1503884728; _ga=ga1.1.1091059248.1504832863"
["accept-language"]=>
string(14) "zh-cn,zh;q=0.8"
["accept-encoding"]=>
string(17) "gzip, deflate, br"
["referer"]=>
string(62) "http://localhost/diannaobengkuifangyunduan/qrcode/headers.html"
["token"]=>//自定义token及值
string(12) "token7758521"
["x-requested-with"]=>
string(14) "xmlhttprequest"
["accept"]=>
string(3) "*/*"
["user-agent"]=>
string(110) "mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/61.0.3163.100 safari/537.36"
["origin"]=>
string(16) "http://localhost"
["connection"]=>
string(5) "close"
["host"]=>
string(9) "localhost"
}
4.原始的headers部分数据
["http_cookie"]=>
string(94) "hm_lvt_f62fa14829605f0d29c05da9c30e045a=1503649309,1503884728; _ga=ga1.1.1091059248.1504832863"
["http_accept_language"]=>
string(14) "zh-cn,zh;q=0.8"
["http_accept_encoding"]=>
string(17) "gzip, deflate, br"
["http_referer"]=>
string(62) "http://localhost/diannaobengkuifangyunduan/qrcode/headers.html"
["http_token"]=>//自定义token及值
string(12) "token7758521"
["http_x_requested_with"]=>
string(14) "xmlhttprequest"
["http_accept"]=>
string(3) "*/*"
["http_user_agent"]=>
string(110) "mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/61.0.3163.100 safari/537.36"
["http_origin"]=>
string(16) "http://localhost"
["content_length"]=>
string(1) "7"
["http_connection"]=>
string(5) "close"
["http_host"]=>
string(9) "localhost"
相关推荐:
php获取文件后缀名的7种方法
php获取当前域名的方法
以上就是php获取ajax的headers方法与内容实例的详细内容。