Redirect in codeigniter after login
- by edelweiss
Trying to do a redirect after a successful login. The login info is sent using ajax to the controller.
My controller code as below
public function login_controller_function()
{
$this->load->model('login_model');
if ($this->input->is_ajax_request())
{
$user_name=$this->input->post('username');
$user_password = $this->input->post('password');
$this->load->helper('url');
$result = $this->login_model->verify_user($user_name,$user_password);
// echo 'user_logged_in';
if(strcmp($result,'user_logged_in')==0)
{
redirect('welcome');
}
}
}
But it is not working at all. Anyone knows whats wrong?
Hi my html as requested. i am using twitter bootstrap as well
<li class="divider-vertical"></li>
<li><a href="#" id="login_btn">Login</a></li>
<li><a href="<?php echo base_url('register'); ?>">Register</a></li>
for the buttons so when i click the login button, a modal window will appear and ask for login info.
so when i click on the login button, my js code will send an ajax request
my js code as below
/*Attempt register user jquery ajax*/
$('#login').click(function(){
var user_name = $('#loginHere').find('#user_name').val();
var user_password = $('#loginHere').find('#login_pwd').val();
if(user_name==""||user_password=="")
return;
var login_data = { username:user_name, password:user_password};
$.ajax({
type: "POST",
dataType: "json",
async: false,
url:"login_register/login_controller_function",
data: login_data,
success: function(data) {
if(data.login)
{
alert(data.redirect);
window.location.replace(data.redirect);
}
else if(!data.login)
{
alert('data login not true');
}
},
error:function(data){
alert('ajax error');
}
});
});
});