这里用到了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';