How to determine whether a Web request is an AJAX request or a normal request in PHP

Source: Internet
Author: User
Tags php script

How can I tell if a Web request is an AJAX request or a normal request in PHP? You can do this by passing parameters, such as using the following URL request:

/path/to/pkphp.com/script.php?ajax

Use the following method in your PHP script to determine:

if(isset($_GET['ajax'])) {
…这是一个ajax请求,然后…
}
else {
…这不是一个ajax请求,然后…
}

By passing the _get parameter, the paper simply realizes the judgment of the Web page request. However, if such a feature is required, the method may be flawed, and the functional requirements are as follows:

1. The Web page requested by Ajax is not the same as the content of the Web page of the ordinary request;

2. The Web page requested by Ajax is for the convenience of the user, the two methods requested to open the page must be the same content, but the AJAX request to the content of the Web page more simplified and use, the removal of the Web page of the large frame template;

3. The purpose of this is: the user in the web operation through Ajax, while the search engine access to the page (equivalent to ordinary open pages), the content is a complete page (including the Web page of the large frame template).

To complete this function, you cannot use the previous pass through get parameter to judge, if you use get pass to judge, user Ajax request and ordinary Web request will be the same content, because you can not set a link with Ajax parameters and no URL. So how do you implement this function? This problem must be resolved through the server-side PHP judgment. That's how PHP judges Ajax requests today. There is a prerequisite for this problem to be solved, and that is that the AJAX framework you use must be jquery. In the jquery framework, when requesting web content through its $.ajax, $.get, or $.post method, it passes a http_x_requested_with parameter to the server, and you can use the following method to determine whether a request is an AJAX request or an ordinary call Please:

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower ($_SERVER
['HTTP_X_REQUESTED_WITH']) == ‘xmlhttprequest’)
{
…这是一个ajax请求,然后…
}
else {
…这不是一个ajax请求,然后…
}

Using this to make a judgment operation, you can keep the URLs of Web pages consistent, but you can get different content for two different requests. That is, the implementation of user operation optimization, and does not affect the search engine included, I think is a good solution!

The other thing to note here is that if your jquery request is open through an IFRAME, the Http_x_requested_with parameter will not be passed, meaning you have no way of judging the type of request.

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.