php擷取ajax的headers方法與內容詳解

來源:互聯網
上載者:User

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"



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.