PHP to determine if it is an AJAX request
For the sake of the security of the program, in some cases we will need to detect whether a request is an AJAX request, how to judge it? This article will briefly share the experience.
1. When using native JavaScript to make AJAX requests, we can add information to the request header to make it easier for the backend PHP program to differentiate, as follows:
var xmlhttp=new xmlhttprequest (); Xmlhttp.open ("GET", "test.php", true); Xmlhttp.setrequestheader ("X-requested-with", "XMLHttpRequest"); Xmlhttp.send ();
Here we add x_requested_with information to the head, the value is XMLHttpRequest, of course, the value here can be arbitrarily set, such as: www.phpernote.com. This can be written on the receiving side of the PHP program:
2. The more popular JS framework jquery is considered more fully, jquery when making an AJAX request, will add a message called X-requested-with in the request header, the information content is: XMLHttpRequest, so the backend PHP You can also use the code as above to make judgments.
Note: If your jquery request is to open a Web page through an iframe, then the Http_x_requested_with parameter is not passed, meaning you have no way of judging the type of the request.
Articles you may be interested in
- How PHP determines whether the current operating system is Linux or Windows
- PHP Determines whether a request is an AJAX request or an ordinary request
- PHP to determine whether the string is in English, pure Chinese, the method of combination of English
- PHP to determine whether the server SSL is enabled, that is, to determine whether to support HTTPS connection
- PHP to determine if a remote file exists
- PHP get_headers to determine if the URL is valid
- How PHP determines if a GIF is a dynamic picture (animated)
- How PHP determines if a constant is defined
http://www.bkjia.com/PHPjc/992745.html www.bkjia.com true http://www.bkjia.com/PHPjc/992745.html techarticle PHP Determines whether the AJAX request is for the sake of the security of the program, in some cases we will need to detect whether a request is an AJAX request, how to judge it? This article will briefly share ...