PHP如何判断一个请求是否来源于本域并且是ajax请求?
PHP如何判断一个请求是否来源于本域并且是ajax请求?
Ajax php https JavaScript http
5152538
9 years, 12 months ago
Answers
jquery中对于ajax是这样写的,xhr对象有setRequestHeader这个方法,设置头部:
if ( !options.crossDomain && !headers["X-Requested-With"] ) {
headers["X-Requested-With"] = "XMLHttpRequest";
}
// Set headers
for ( i in headers ) {
xhr.setRequestHeader( i, headers[ i ] );
}
所以如果在PHP端验证的话,是这样的:
public static function isAjax() {
return 'XMLHttpRequest' == @$_SERVER['HTTP_X_REQUESTED_WITH'];
}
二小姐蕩漾
answered 9 years, 12 months ago