PHP to determine whether a request is an AJAX request or an ordinary request _php tutorial

Source: Internet
Author: User
In a PHP program, how to tell if a Web request is an AJAX request or a normal request? We often have such a problem in the project, the browser can directly input the address of my Ajax submission, the browsers will be able to directly request data and print data to the page. From the rigor and safety of the procedure, I find this very bad.

However, due to the limited level, this problem was not solved until today, I hereby leave the article for those who have the need to learn.

First of all, the principle: When sending an AJAX request, we can create a custom header message by XMLHttpRequest This object, if you are using a native Ajax method, that is, an Ajax method that is not wrapped with jquery or another JS framework, Then the code is as follows:

Xmlhttprequest.setrequestheader ("Request_type", "Ajax");

In addition, with the $.ajax () method of the jquery wrapper, we can easily create our own custom header information before sending an AJAX request, as shown in the following example:

$.ajax ({type: "GET", Url:base_url + ' php_check_ajax_request/get_user_list.html ', beforesend:function (XMLHttpRequest {Xmlhttprequest.setrequestheader ("Request_type", "Ajax"),},success:function (data) {$ ("#user_list"). HTML (data); $ Tip.hide (); $button. attr (' disabled ', true);});

There is a sentence in the above code:

Xmlhttprequest.setrequestheader ("Request_type", "Ajax");

This code is to create a custom variable "Request_type" into the header information. This variable receives the variable in PHP as follows:

$_server[' Http_request_type ']

So, for the problems raised in this article, we can determine the way a user sends a request in this way.

 

It should be stated that the value of the variable "Request_type" is custom, and you can customize it arbitrarily, such as "Test", "Is_ajax" and so on.

Articles you may be interested in

    • Differences between synchronous requests and asynchronous requests in HTTP requests
    • PHP to determine whether the string is in English, pure Chinese, the method of combination of English
    • How PHP determines whether the current operating system is Linux or Windows
    • JS how to determine whether the mouse wheel is down or scroll up
    • Summary of how PHP determines the correct date format
    • PHP to determine if a remote file exists
    • A classic dialogue between programmers and testers. These are the foreign programmers to share, said the global general?
    • PHP Extract the birthday date from the ID number and the function to verify whether it is a minor

http://www.bkjia.com/PHPjc/764108.html www.bkjia.com true http://www.bkjia.com/PHPjc/764108.html techarticle In a PHP program, how to tell if a Web request is an AJAX request or a normal request? We often have such a problem in the project, in the browser directly input my Ajax submitted ...

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