PHP Determines whether a request is an AJAX request or a normal request

Source: Internet
Author: User
Tags date header variable

In the PHP program, how to determine whether a Web request is an AJAX request or a normal request? We often have a problem with the project, in the browser directly input my Ajax submitted address, the browser can directly request the data and print the data on the page. In terms of procedural rigor and security, I think it's very bad.

But because of the limited level, this problem until today to be solved by me, I hereby leave the article for those who need to learn.

First, the principle: When sending Ajax requests, we can create custom header headers by XMLHttpRequest This object, if you are using native Ajax methods, that is, Ajax methods that are not packaged with jquery or other JS frameworks, So the code is as follows:

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

Also through the $.ajax () method of the jquery wrapper, we can easily create our custom header header information before we send the 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 one sentence in the above code:

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

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

$_server[' Http_request_type ']

So for the questions that are raised in this article, we can do the following to determine the type of request that the user sent.

<?php
if (isset ($_server[' Http_request_type ')) && $_server[' http_request_type '] = = "Ajax") {
	// Ajax Submit
}else{
	//non-AJAX submission
}

What needs to be explained is that the "request_type" variable value is our custom, you can also customize, such as "Test", "Is_ajax" and so on.

Articles that you may be interested in

    • Differences between synchronous and asynchronous requests in HTTP requests
    • PHP to determine whether the string is full English, pure Chinese, in combination with the English method
    • How PHP determines whether the current operating system is Linux or Windows
    • JS how to determine whether the mouse wheel is scrolling down or up
    • A summary of how PHP determines whether the date format is correct
    • PHP to determine whether a remote file exists
    • PHP extracts the birthday date in the ID number and the function to verify that it is a minor
    • Classic dialogue between programmers and testers. These are foreign programmers summed up the share, called the global GM?


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.