首頁 php教程 php手册 php验证请求页面来源

php验证请求页面来源

May 25, 2016 pm 04:46 PM

php验证请求页面来源:

if( $_server['http_x_requested_with'] == 'xmlhttprequest' ) { 
    echo 'ajax'; 
} else { 
    echo 'normal'; 
}
登入後複製

jquery内部实现ajax的时候,已经加入了标识,jquery源码中是这样的:

xhr.setrequestheader("x-requested-with", "xmlhttprequest");
登入後複製

所以,在php中可以通过http_x_requested_with来判断,不需要另外实现,下面看原理:在发送ajax请求的时候,我们可以通过xmlhttprequest这个对象,创建自定义的header头信息,如果您使用的是原生的ajax方法,也就是未使用jquery或者其他js框架包装的ajax方法,那么代码如下:

xmlhttprequest.setrequestheader("request_type","ajax");

通过jquery的$.ajax()方法,可以轻松地在发送ajax请求之前,创建我们自定义的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获取时:

if (isset($_server['http_request_type']) && $_server['http_request_type'] 
== "ajax"){//ajax提交 
}else{//非ajax提交 
}
登入後複製


本文链接:

收藏随意^^请保留教程地址.

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)