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

Source: Internet
Author: User
/path/to/pkphp.com/script.php?ajax
Use the following method in the 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 ...
}

The method of passing _get parameter is simple to realize the Judgment of Web page request. However, if such a function is required, this method may have drawbacks, and the functional requirements are as follows:
1. The Web page requested by Ajax is not the same as the content of the normal request.
2. The Web page requested by Ajax is for user-friendly operation, the two methods request to open the page must be the same content, just AJAX request to the page content simplification and use, removed the Web page of the large frame template.
3. The purpose of this is to: the user in the Web page operation through the AJAX implementation, and the search engine access to the Web page (equivalent to the normal opening of the page), the content is a complete page (including the Web page of the large framework template).
To complete this function, you cannot use the get parameter passed as described above, if you use get pass to judge, the user Ajax requests and ordinary Web page requests will be the same content, because you can not set a link with Ajax parameters and no URL. So how can this function be implemented? This problem must be resolved through server-side PHP judgment. That's how PHP judges Ajax requests today. There is a prerequisite to solving this problem, and that is that the AJAX framework you use must be jquery. In the jquery framework, when requesting web page 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 make the URL of the Web page consistent, but can be two different requests can get different content of the page. That realizes the user operation optimization, but does not affect the search engine to ingest, I think is a great solution!
Here's another thing to keep in mind: If your jquery request is to open a Web page through an iframe, then the Http_x_requested_with parameter is not passed, which means you have no way of judging the type of the request.

The main contents are:

1.
There is a prerequisite to solving this problem, and that is that the AJAX framework you use must be jquery. In the jquery framework, when requesting web page 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.
Here's another thing to keep in mind: If your jquery request is to open a Web page through an iframe, then the Http_x_requested_with parameter is not passed, which means you have no way of judging the type of the request.

http://www.bkjia.com/PHPjc/323734.html www.bkjia.com true http://www.bkjia.com/PHPjc/323734.html techarticle /path/to/pkphp.com/script.php?ajax in a PHP script using the following method: Copy code code as follows: if (Isset ($_get[' Ajax ")) {... This is an AJAX request, and then ...} else {...

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