How to Check If the Request Is an Ajax Request With PHP

How to check if the request is an AJAX request with PHP

There is no sure-fire way of knowing that a request was made via Ajax. You can never trust data coming from the client. You could use a couple of different methods but they can be easily overcome by spoofing.

How to find out if a request is an ajax request?

There's no 100% way to detect if the request was made via ajax. Even if someone sends header with "X-Requested-With: XMLHttpRequest" you shouldn't rely on it.

How to check if the ajax call method is POST or GET?

Just try with:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
// POST
} else {
// GET
}

Laravel - check if Ajax request

Maybe this helps. You have to refer the @param

         /**       
* Display a listing of the resource.
*
* @param Illuminate\Http\Request $request
* @return Response
*/
public function index(Request $request)
{
if($request->ajax()){
return "AJAX";
}
return "HTTP";
}

How to find what file made an ajax request with php

You could add a custom header when executing your request using AJAX. Then check if your custom header is present in get_headers().

If your custom header is present, the request come from your js file. If not, it's a regular request.

You can check how to add a custom header here.

How do I check if the request is made via AJAX in CodeIgniter?

If you are using a library that sends the X-Requested-With header, then you can do...

if (strtolower(filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest') {
// I'm AJAX!
}


Related Topics



Leave a reply



Submit