PHP to determine if the method of Ajax request _php Tips

Source: Internet
Author: User

The example in this article describes the way PHP is judged to be an AJAX request. Share to everyone for your reference, specific as follows:

First of all, how to distinguish the front end using JQuery:

When JQuery makes an AJAX request, it adds a message named X-requested-with to the request header, which reads: XMLHttpRequest

You can use $_server["Http_x_requested_with" on the back end to get it. (Note: The underscore is underlined, case-insensitive)

From this, we can determine whether the AJAX request:

if (Isset ($_server["Http_x_requested_with"]) && strtolower ($_server["Http_x_requested_with"]) = = " XMLHttpRequest ") { 
  //Ajax request processing mode 
}else{ 
  //Normal request processing mode 
};

When Ajax requests are made using native JavaScript, we can also add information to the head to make it easier for the back-end students to distinguish between the following methods:

var xmlhttp=new xmlhttprequest (); 
Xmlhttp.open ("Get", "test.php", true); 
Xmlhttp.setrequestheader ("X-requested-with", "XMLHttpRequest"); 
Xmlhttp.send ();

For more information about PHP interested readers can view the site topics: "Php+ajax Skills and Application Summary", "PHP Network Programming Skills Summary", "PHP object-oriented Program Design Introduction Tutorial", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.