Codeigniter session not saving userdata and creating duplicate sessions
I'm trying to send my username and password through ajax to the database
for verifying the user but I see CI is creating duplicate sessions for a
single request. Also when I'm trying to set the userdata through
session->set_userdata it is not setting the data. This is confirmed as
neither firebug nor my session database shows the data. Also since no data
is getting set in the session I do not get anything back in my ajax
response to show in the ajax response success
Please guide me what am I doing wrong here as the session cookies are
created even if the username and password combination is wrong.
model
function validate_login()
{
$this->db->where('Useremail',$this->input->post('username'));
$this->db->where('Password',$this->input->post('password'));
$result = $this->db->get('users');
if ($result->num_rows == 1) {
return true;
}else{
return false;
}
}
controller
function validate_login_user()
{
$this->load->model('data_model');
if ($this->data_model->validate_login())
{
$data = array (
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
return = $this->session->userdata('username');
}
else
{
return false;
}
}
MY view
<script>
$(function(){
$('.login').click(function(){
var username = $('#username').attr('value');
var password = $('#password').attr('value');
if (username == "" || password == ""){
$('.LoginErrormsg').css('display','block');
}
if (username !== "" && password !== ""){
$.ajax({
url:"<?php echo site_url('home/validate_login_user');?>",
type:'POST',
data:{username:username,
password:password},
success:function(){
$('#sitelogin').html(msg);
$('.logout').css('display','block');
}
});
return false
}else{
return false;
}
});
})
<div id="sitelogin">
<input type="text" class="loginbox" name = "login_username" id =
"username" placeholder="Email address / Username" />
<input type="password" class="loginbox" name = "login_password"
id="password" placeholder="Password" />
<div class="LoginErrormsg">You forgot to enter Username and
Password</div>
<button class="login" name = "login">Login</button>
<button class="logout" name = "logout">Logout</button>
</div>
No comments:
Post a Comment