I’m in a PHP script and I want to check whether the request is an Ajax request. (Basically to NOT allow direct script access, other than Ajax calls.)
So, I’m defining IS_AJAX somewhere in the main index.php file:
define('IS_AJAX',
isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
And then checking it at the top of my script:
if (!IS_AJAX) exit('No direct script access allowed');
Since I’m new to CodeIgniter, I would like to know:
- Is there any such built-in functionality?
- Is there a more elegant way to do it?
You can use
$this->input->is_ajax_request()from the input class: