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

Source: Internet
Author: User
Tags php script
/path/to/pkphp.com/script.php?ajax
Use the following method in your PHP script to determine:
Copy CodeThe code is as follows:
if (Isset ($_get[' ajax ')) {
... This is an AJAX request, and then ...
}
else {
... This is not an AJAX request, and then ...
}

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. Web pages requested through Ajax are not the same as those of a normal request.
2. The Web page requested by Ajax is for the convenience of the user, the two methods requested to open the Web page must be the same content, but the AJAX request to the content of the Web page to simplify and use, the removal of the page's 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, You can use the following method to determine whether a request is an AJAX request or a normal request:
if (Isset ($_server[' Http_x_requested_with ')) && strtolower ($_server[' http_x_requested_with ']) = = ' XMLHttpRequest ')
{
.. This is an AJAX request, and then ...
}
else {
.. This is not an AJAX request, and then ...
}
if (Isset ($_server[' Http_x_requested_with ')) && strtolower ($_server[' http_x_requested_with ']) = = ' XMLHttpRequest ') if (isset ($_server[' Http_x_requested_with ')) && strtolower ($_server[' http_x_requested_ With
{
... This is an AJAX request, and then ...
}
else {
... This is not an AJAX request, and then ...
}
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.

The main contents are:

1.
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, You can use the following method to determine whether a request is an AJAX request or a normal request:
if (Isset ($_server[' Http_x_requested_with ')) && strtolower ($_server[' http_x_requested_with ']) = = ' XMLHttpRequest ')
{

2.
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.