How to get headers in ajax in PHP (case)

墨辰丷
Release: 2023-03-26 07:22:02
Original
2903 people have browsed it

This article mainly introduces how to obtain headers in ajax in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

1. Front-end page

<!DOCTYPE html>
<html>
<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: &#39;headers.php&#39;,
data: {&#39;key&#39;:&#39;123&#39;},//如果需要字符串json方式,请使用JSON.stringify(setting.data)
type: &#39;POST&#39;,
//dataType: &#39;json&#39;,
//contentType: &#39;application/json&#39;,
//processData: true,//为true不会序列化数据
beforeSend: function () {
// layer.load(2);
},
headers: {
"token":&#39;token7758521&#39;//自定义token及值
},
success: function (responseData) {
console.log(responseData);
},
error: function (error) {
console.log(error);
}
});
});
</script>
</body>
Copy after login

2.php page

<?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) == &#39;HTTP_&#39;) //前5个字符是HTTP_的进入循环
 { 
 $headers[str_replace(&#39; &#39;, &#39;-&#39;, ucwords(strtolower(str_replace(&#39;_&#39;, &#39; &#39;, substr($name, 5)))))] = $value; 
 //注释
 //substr($name, 5),从$name第5个字符向后截取
 //str_replace(&#39;_&#39;, &#39; &#39;,)下划线替换成空格
 //strtolower()全部转换为小写
 //ucwords()首字母转换为大写
 //str_replace(&#39; &#39;, &#39;-&#39;,)所有空格替换为-
 } 
 } 
 return $headers; //返回前key前5个字符是HTTP_的数组
 //return $_SERVER; //返回_SERVER数组
}
Copy after login

3. Headers returned by the php page

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"
}
Copy after login

4. Original headers partial data

["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"
Copy after login

Related recommendations:

php Get ajax headers method and content example

get_headers and curl in php Detailed explanation of the difference

jQuey Form implements Ajax to upload files and sets headers at the same time

The above is the detailed content of How to get headers in ajax in PHP (case). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template