Method for determining whether a request is an ajax request or a common request in php _ PHP Tutorial

Source: Internet
Author: User
In php, you can determine whether a request is an ajax request or a common request. Pathtopkphp. comscript. php? Ajax uses the following method in the php script: the code for copying the code is as follows: if (isset ($ _ GET [ajax]) {... this is an ajax request, and then ...} else {.. /path/to/pkphp.com/script.php? Ajax
Use the following method in the php script:

The 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 webpage request is judged simply. However, if such a function is required, this method may have drawbacks. the functional requirements are as follows:
1. the content of the webpage requested through ajax is different from that of the webpage requested by common requests.
2. the webpage requested by ajax is used to facilitate user operations. the content of the webpage requested by the two methods is the same, but the content of the webpage requested by ajax is simplified and used, remove the large frame template of the webpage.
3. the purpose of this operation is to use ajax for webpage operations, while when a search engine accesses a webpage (equivalent to opening a webpage ), the obtained content is a complete webpage (including the large frame template of the webpage ).
To complete the above function, you cannot use the GET parameter transfer described earlier to determine whether to use the GET pass function, user ajax requests and common webpage requests are the same content, because you cannot set a URL with ajax judgment parameters or without parameters for a link. How can this function be implemented? This problem must be solved through the PHP judgment on the server side. That is, how PHP judges ajax requests today. A prerequisite for solving this problem is that the ajax framework you use must be jquery. In the jquery framework. ajax, $. get, or $. when the post method requests the webpage content, it will pass an 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 common 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...
}
By using this method, the URL of the webpage can be consistent, but the webpage with different content can be obtained for two different requests. That is to say, it achieves user operation optimization without affecting search engine indexing. I think it is a great solution!
Another issue that needs to be noted here is that if your jquery request opens a webpage through iframe, the HTTP_X_REQUESTED_WITH parameter will not be passed, that is, you cannot determine the request type.

The main content is:

1.
A prerequisite for solving this problem is that the ajax framework you use must be jquery. In the jquery framework. ajax, $. get, or $. when the post method requests the webpage content, it will pass an 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 common request:
If (isset ($ _ SERVER ['http _ X_REQUESTED_WITH ']) & strtolower ($ _ SERVER ['http _ X_REQUESTED_WITH']) = 'xmlhttprequest ')
{

2.
Another issue that needs to be noted here is that if your jquery request opens a webpage through iframe, the HTTP_X_REQUESTED_WITH parameter will not be passed, that is, you cannot determine the request type.

Http://www.bkjia.com/PHPjc/323734.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323734.htmlTechArticle/path/to/pkphp.com/script.php? Ajax uses the following method in the php script: the code is as follows: if (isset ($ _ GET ['Ajax ']) {... this is an ajax request, and then ...} else {.....

Related Article

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.