Joke Collection Website - Public benefit messages - php determines whether it is an ajax request

php determines whether it is an ajax request

1. Simply implement the judgment of web page requests by passing the _GET parameter.

This is passed on the url: *******.

php?ajaxphp determines this:

if(isset($_GET['ajax' ])) {

...This is an ajax request, then...}else {

...This is not an ajax request, then...} This The implementation is poor and very easy to fake. Is there any difference between judging and not judging.

So there is another way of handling things that I think is very scientific. This method does have certain learning value.

Php obtains the xmlHttpRequest interpretation in the system variable.

First of all, you must use jquery. JS sends an ajax request. When requesting web content through the $.ajax, $.get, or $.post method sent by jquery, it will pass an HTTP_X_REQUESTED_WITH parameter to the server. The value of this parameter is xmlHttpRequest.

Js code: The code is as follows. Copy the code beforeSend: function (XMLHttpRequest) {

XMLHttpRequest.setRequestHeader(X-Requested-With,XMLHttpRequest);}; parameter name X-Requested-With , parameter value XMLHttpRequest, you can define it arbitrarily. When obtaining Php, add capitalized http in front of the parameter name. Copy the code as follows if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){< /p>

//If AJAX Request Then}else{

//something else}

1. If your jquery request opens the web page through an iframe, then HTTP_X_REQUESTED_WITH Parameters are not passed, which means you have no way to determine the type of request.