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.
- Related articles
- What do you say when you just joined the party branch WeChat group?
- Yi people kill pig rice in the New Year (annual flavor)
- China Mobile sends a text message to check the number? (For example, 10086 rpm 192 is traffic monitoring) Find 192 = * *?
- Why can't I send text messages with vivo?
- Taobao buyer has received the goods, but the seller's logistics information does not show the receipt, and the buyer does not confirm the receipt.
- What software can make iPhone and HTC send short messages for free?
- How to check the power failure?
- Introduction of Han Yu's Great Artist in Orange Light Game Kim Woo Bin He's Brief Introduction of Han Yu's Great Artist in Orange Light Game
- Does every vivo phone have a super power saving mode? In other words, when this mode is on, you can only send and receive telephone messages, and a battery can stand by for more than ten hours.
- How to find Changsha Bank to pay?