PHP judges whether a request is an AJAX request or a common request _ PHP Tutorial

Source: Internet
Author: User
PHP judges whether a request is an AJAX request or a common request. In a PHP program, how does one determine whether a webpage request is an ajax request or a common request? We often have this problem in projects. in a browser, I directly input my ajax submitted in the PHP program. how can I determine whether a webpage request is an ajax request or a common request? This problem often exists in projects. after entering the address submitted by my ajax in the browser, the browser can directly request data and print the data to the page. In terms of program rigor and security, I think this is very bad.

However, due to the limited level, this problem was not solved until today. I will leave an article for anyone who needs it to learn.

First, let's talk about the principle: when sending an ajax request, we can use the XMLHttpRequest object to create custom header information. if you are using a native ajax method, that is, the ajax method is not packaged using jquery or other js frameworks. the code is as follows:

xmlHttpRequest.setRequestHeader("request_type","ajax");

In addition, using the $. ajax () method encapsulated by jquery, we can easily create custom header information before sending an ajax request, for 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);}});

The above code contains the following sentence:

XMLHttpRequest.setRequestHeader(“request_type”,”ajax”);

This code creates a custom variable "request_type" to the header information ". The variable received in php is as follows:

$_SERVER['HTTP_REQUEST_TYPE']

For the questions raised in this article, we can use the following method to determine which method the user sends the request.

 

It should be noted that the variable value "request_type" is customized and can be customized at will, such as "test" and "is_ajax.

Articles you may be interested in
  • Differences between synchronous requests and asynchronous requests in http requests
  • Php determines whether the string is full of English, pure Chinese, and a combination of Chinese and English
  • How does php determine whether the current operating system is linux or windows?
  • How does js determine whether the scroll wheel is scroll down or up?
  • Summary of methods for php to determine whether the date format is correct
  • Php methods to determine whether a remote file exists
  • A classic dialogue between programmers and testers. These are shared by foreign programmers, who say they are used globally?
  • Php extracts the birthday date from the ID card number and verifies whether it is a minor.

Why? We often have such a problem in our projects. in the browser, we directly input the content submitted by my ajax...

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.