In PHP, how does one determine whether a webpage request is an ajax request or a common request?

Source: Internet
Author: User
In PHP, how does one determine whether a webpage request is an ajax request or a common request? You can achieve this by passing parameters, for example, using the following URL request: pathtopkphp. comscript. php? Ajax uses the following method in the PHP script: if (isset ($ _ GET [ajax]) {… This is an ajax request, and then ...} Else {... This

In PHP, how does one determine whether a webpage request is an ajax request or a common request? You can achieve this by passing parameters, for example, using the following URL request:/path/to/pkphp.com/script.php? Ajax uses the following method in the PHP script: if (isset ($ _ GET ['ajax ']) {… This is an ajax request, and then... } Else {... This

In PHP, how does one determine whether a webpage request is an ajax request or a common request? You can achieve this by passing parameters, for example, using the following URL request:

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

Use the following method in the PHP script:

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 server PHP judgment. 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...
}

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.

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.