Php determines if it is an ajax request

Source: Internet
Author: User
Tags mootools
Most JavaScript frameworks, such as jquery and Mootools, send additional HTTP_X_REQUESTED_WITH header information when sending Ajax requests. the header is an ajax request, therefore, you can detect Ajax requests on the server.

Most JavaScript frameworks, such as jquery and Mootools, send additional HTTP_X_REQUESTED_WITH header information when sending Ajax requests. the header is an ajax request, therefore, you can detect Ajax requests on the server side.

1. pass the _ GET parameter to determine the webpage request.

The url is passed as follows: *******. php? Ajax

Php's judgment:

  1. If (isset ($ _ GET ['Ajax ']) {
  2.  
  3. ... This is an ajax request, and then...
  4.  
  5. }
  6.  
  7. Else {
  8.  
  9. ... This is not an ajax request, and then...
  10.  
  11. }

This implementation method is very poor, and it is quite easy to forge. there is no difference between judgment and non-judgment, so another method that we think is very scientific is proposed, this method does have some learning value.

Php obtains the xmlHttpRequest interpretation in the system variables.

First, you must use jquery and Js to send ajax requests. ajax, $. get, or $. when the post method requests the webpage content, it will pass a HTTP_X_REQUESTED_WITH parameter to the server. The value of this parameter is xmlHttpRequest.

The Js code is as follows:

  1. BeforeSend: function (XMLHttpRequest ){
  2.  
  3. XMLHttpRequest. setRequestHeader ("X-Requested-With", "XMLHttpRequest ");
  4.  
  5. };
The parameter name is X-Requested-With, and the parameter value is XMLHttpRequest. you can define it as needed. when obtaining the parameter name in Php, you must add an http character in front of the parameter name. The code is as follows:
  1. If (! Emptyempty ($ _ SERVER ['http _ X_REQUESTED_WITH ']) & strtolower ($ _ SERVER ['http _ X_REQUESTED_WITH']) = 'xmlhttprequest ')
  2. {
  3. // If AJAX Request Then
  4. } Else {
  5. // Something else
  6. }

1. if

If your jquery request uses iframe to open a webpage, the HTTP_X_REQUESTED_WITH parameter will not be passed. that is to say, you cannot determine the request type.

2. Internet Explorer 5 and Internet Explorer 6 versions $. ajax, $. get, or $. the post method does not automatically send the HTTP_X_REQUESTED_WITH parameter to the header information, because the parameter cannot be obtained in php for judgment, unless you construct the XMLHttpRequest object and send it to the php script, it is equivalent to implementing ajax functions with the most primitive JavaScript code. in this way, the ajax framework originally encapsulated by jquery will become meaningless.

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.