Home  >  Article  >  Backend Development  >  How to determine if a request is an ajax request?

How to determine if a request is an ajax request?

WBOY
WBOYOriginal
2016-08-08 09:22:381560browse

How to determine if a request is an ajax request?

1. We can judge by the X-Requested-With in the http protocol header information

2. If the ajax request is completed using jquery, there will be an HTTP-X-REQUESTED-WITH key value in $_SERVER. You can judge by this

For example:

if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'){
    $username = isset($_POST['username']) ? addslashes($_POST['username']) : '';
    $passwd = isset($_POST['passwd']) ? addslashes($_POST['passwd']) : '';
    if($username == 'lh' && $passwd == 'passwd'){
        echo 'hello baidu!';
    }
}else{
    echo 'you must use ajax request!';
}

If it is an ajax request, what will be the referer corresponding to the request header information?

If it is an ajax request, the referer parameter corresponding to the request header information is the url of the current page

The above introduces how to determine whether a request is an ajax request? , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn