php判断是否为ajax请求:


function isAjax(){
    if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"])=="xmlhttprequest"){
        return true;
    }else{
        return false;
    }
}

对应的javascript代码:


var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","test.php",true);
xmlhttp.setRequestHeader("X-Requested-With","XMLHttpRequest");
xmlhttp.send();