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
Transform Relative Path into Absolute Url Using PHP
PHP/MySQL With Multiple Queries
Required Parameter $Xxx Follows Optional Parameter $Yyy
Warning: MySQL_Fetch_Array() Expects Parameter 1 to Be Resource, Boolean Given In
How Is the PHP Array Implemented on the C Level
Writing a New Line to File in PHP (Line Feed)
Converting an Integer to a String in PHP
PHP Image Upload Security Check List
Get the Current Script File Name
How to Re-Index All Subarray Elements of a Multidimensional Array
How to Debug Pdo Database Queries
Count How Many Files in Directory PHP
Osx 10.10 Curl Post to Https Url Gives Sslread() Error
How to Loop Through a MySQL Result Set More Than Once Using the MySQL_* Functions