The method in php to determine whether a request is an ajax request or a normal request

  • 2020-05-09 18:21:21
  • OfStack

/path/to/pkphp.com/script.php?ajax
In the php script, the following method is used:
 
if(isset($_GET['ajax'])) { 
... This is a 1 a ajax Request, and then ... 
} 
else { 
... This is not 1 a ajax Request, and then ... 
} 

The method of passing _GET parameter simply realizes the judgment of web page request. However, if such functionality is required, this method may have drawbacks. The functional requirements are as follows:
1. The web page requested through ajax is different from the normal web page content
2. The web page requested through ajax is for the convenience of user operation. The content required to open the web page requested by the two methods is the same, but the web content requested by ajax is simplified and used without the big frame template of the web page.
3. The purpose of this is: the user can use ajax to realize the web page operation, and when the search engine visits the web page (equivalent to the normal opening of the web page), the content obtained is a complete web page (including the large frame template of the web page).
To complete this function, you cannot use the GET parameter pass to judge, if you use GET pass to judge, the user ajax request and the normal web request will be the same content, because you cannot set 1 ajax judge parameter and no URL for 1 link. So how do you do this? This problem must be resolved by server-side PHP judgment. That's how PHP judges ajax requests today. One prerequisite to solving this problem is that the ajax framework you are using must be jquery. In the jquery framework, when you request web content through its $.ajax, $.get, or $.post methods, it passes 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 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 as a judgment call, you can keep the URL on the page at 1, but you can get different pages with different content for two different requests. That is to achieve user operation optimization, and does not affect the search engine included, I think it is a great solution!
One other thing to note here is that if your jquery request is opened via iframe, then the HTTP_X_REQUESTED_WITH parameter will not be passed, which means you have no way to determine the type of request.

The main content is:

1.
One prerequisite to solving this problem is that the ajax framework you are using must be jquery. In the jquery framework, when it requests web content through its $.ajax, $.get, or $.post methods, it passes 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 normal request:
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) & & strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{

2.
One additional thing to note here is that if your jquery request is opened via iframe, then the HTTP_X_REQUESTED_WITH parameter will not be passed, which means you have no way to determine the type of request.

Related articles: