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:
Copy Code code 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:
Copy Code code as follows:
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 ...
}
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!
There is another issue to note here, which is if your jquery request opens the page through an IFRAME, then the Http_x_requested_with parameter will not be passed , That means you have no way of judging the type of request.