title: Check for Ajax request in CodeIgniter
body: <p>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.)</p>
  
  <p>So, I'm defining <code>IS_AJAX</code> somewhere in the main <code>index.php</code> file:</p>
  
  <pre><code>define('IS_AJAX', 
         isset($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; 
         strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
  </code></pre>
  
  <p>And then checking it at the top of my script:</p>
  
  <pre><code>if (!IS_AJAX) exit('No direct script access allowed');
  </code></pre>
  
  <p>Since I'm new to CodeIgniter, I would like to know:</p>
  
  <ul>
  <li>Is there any such built-in functionality?</li>
  <li>Is there a more elegant way to do it?</li>
  </ul>