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 to request Ajax
/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.