php请求来源,php验证请求页面来源
php教程驗(yàn)證請(qǐng)求頁(yè)面來(lái)源
if( $_server['http_x_requested_with'] == 'xmlhttprequest' ) {
echo 'ajax';
} else {
echo 'normal';
}
jquery內(nèi)部實(shí)現(xiàn)ajax的時(shí)候,已經(jīng)加入了標(biāo)識(shí)
jquery源碼中是這樣的:
xhr.setrequestheader("x-requested-with", "xmlhttprequest");
所以,在php中可以通過(guò)http_x_requested_with來(lái)判斷,不需要另外實(shí)現(xiàn) :)
下面看原理
在發(fā)送ajax請(qǐng)求的時(shí)候,我們可以通過(guò)xmlhttprequest這個(gè)對(duì)象,創(chuàng)建自
定義的header頭信息
如果您使用的是原生的ajax方法,也就是未使用jquery或者其他js框架包裝的ajax
方法,那么代碼如下:
xmlhttprequest.setrequestheader("request_type","ajax");
通過(guò)jquery的$.ajax()方法,可以輕松地在發(fā)送ajax請(qǐng)求之前,創(chuàng)建我們自定義的
header頭信息。
$.ajax({
type??????????????? : "get",
url??????????????? : base_url
'php_check_ajax_request/get_user_list.html',
beforesend???????? : function (xmlhttprequest) {
xmlhttprequest.setrequestheader("request_type","ajax");
},
success???????? : function(data){
$("#user_list").html(data);
$tip.hide();
$button.attr('disabled',true);
}
});
在php獲取時(shí)
if (isset($_server['http_request_type']) && $_server['http_request_type']
== "ajax"){//ajax提交
}else{//非ajax提交
}
總結(jié)
以上是生活随笔為你收集整理的php请求来源,php验证请求页面来源的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 安装linux出现基础系统出错,Linu
- 下一篇: android 伪造gps位置,在And