public function user_login()
{
$email = $_POST['email'];
$password = $_POST['password'];
$userdata = $this->em->user_login($email, $password);
$user_info = $this->em->does_user_exist($email, $password);
if ($user_info->num_rows() == 1) {
foreach ($user_info->result() as $row) {
$data = array(
'id' => $row->id,
'username' => $row->username,
'email' => $row->email,
'user_type' => $row->user_type,
'logged_in' => TRUE
);
$this->session->set_userdata($data);
}
if($userdata) {
echo 1;
} else {
echo 'error';
}
}
}
这是我的控制器,我检查用户是否可以登录,如果用户存在则用户存在我获取所有数据并设置会话。出于某种原因,当我回显会话用户名时,它不会显示。可能有什么不对?
这是我如何回应会话:
<div>
hello<p><?php echo $this->session->userdata('username'); ?></p>
</div>
<div>
<p><?php echo $data['username']; ?></p>
</div>
还有其他方法来回应会话吗?
答案 0 :(得分:0)
第一名:根据您的评论,您可以返回用户名作为对ajax的回复
if($userdata) {
echo json_encode(array('success'=>1,'username'=>$this->session->userdata('username')));
} else {
echo json_encode(array('success'=>0));
}
第二名:并像这样以ajax成功更新DOM。
success:function(response){
if(response.success==1){
$('#div_id').html(response.username);
}else{
$('#div_id').html('error');
}
}
第3名在你的ajax中,dataType应该是json
dataType:'json'
答案 1 :(得分:0)
尝试像这样设置会话:
if ($qa->num_rows() > 0) {
$resData = $qa->row();
$userdata = array(
'user_id' => $resData->user_id,
'user_name' => ($resData->username),
'parent_user_id' => ($resData->parent_user_id),
'email_id' => ($resData->email_id),
'per_page' => '20'
);
$this->session->set_userdata($userdata);
return true;
}