欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

[php] php如何判断是不是ajax提交

程序员文章站 2022-05-03 19:41:23
...
这里用到了jquery的ajax。

页面:

$.ajax({
type: 'POST',
url: 'http://www.yourwebsite.com',
beforeSend: function (XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("request_type","ajax");
},
data: 'test=test',
success: function(rs) {
alert('ok');
}
}
});

PHP代码:

echo isset( $_SERVER['HTTP_REQUEST_TYPE']) && $_SERVER['HTTP_REQUEST_TYPE'] == "ajax" ? "it is ajax" : "it is NOT ajax.";

还有一种方法,js里面不用加beforeSend里面的代码,直接在PHP里面作判断:

echo isset( $_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest' ? 'it is ajax' : 'it is NOT ajax';

PS:判断是否是Flash提交数据

echo isset( $_SERVER['HTTP_USER_AGENT']) && ( stripos( $_SERVER['HTTP_USER_AGENT'],'Shockwave')!== false || stripos( $_SERVER['HTTP_USER_AGENT'],'Flash')!== false) ? echo 'it is flash data' : 'it is NOT flash data';