PHP to determine if it is an AJAX request _php tutorial

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.