Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How do I check if the request is an AJAX? I am using CodeIgniter. I have a link that when it clicked, it'll open the pop-up dialog window this is done through ajax it requests to a controller name login_window().

CodeIgniter

//Here is the controller name: function login_window(){     // request via ajax     $this->load->view("login_window"); } 

jQuery

//here is the jquery code: //I am using a jquery plugin FACEBOX  $('a[rel*=dialog]').facebox();  <a href="http://localhost/codeigniter/login_window" rel="dialog">Login</a> 

I want to check if it is an AJAX request and if not, i will redirect them to homepage. so there's no way they can access the page that is intended only for ajax requests.

like image 372
jameserie Avatar asked Nov 29 '10 05:11

jameserie


1 Answers

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! } 
like image 98
alex Avatar answered Sep 23 '22 19:09

alex