Leandro Garcia
Leandro Garcia

Reputation: 3228

protect ajax jquery

I use this to check in my registration page, if the email already exists on my database:

$('#email').blur(function() {
  var email = $(this).val();
  $.post('ajax/get_request/check',{key:"email",value:email},function(data) {
    alert(data);
  });
});

It does works fine. However, I've noticed, if I access the ajax path via URL address bar of the browser, http://example.com/ajax/get_request/check, this displays blank. I tried this script to redirect if value is null.

//controller.php
public function check($key=NULL,$value=NULL)
{
  if(is_null($value)) redirect(base_url(),'refresh');
  // ... more scripts below ...
}

But this affect my ajax request response on registration. What I want to achieve here is to detect if the user access the ajax via registration form not when directly typing the path on the URL browser.

Upvotes: 0

Views: 161

Answers (1)

Broncha
Broncha

Reputation: 3794

you can use this

$this->input->is_ajax_request()

to check if the incoming request is an ajax request. This is available in codeigniter2

In your controller,

if($this->input->is_ajax_request()){
    //do your stuffs
}else{
  die("Access Prohibited.");
}

Upvotes: 2

Related Questions