dtgr6303 2017-08-17 04:16
浏览 86

无法将$ _SESSION中的用户名回显到页面

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';
        }
    }
}

This is my controller where I check if user can login and does user exist if they exist I get the all of the data and set the session. For some reason when I echo the the session username it wont display. What could be wrong?

Here's how I echo the session:

<div>
  hello<p><?php echo $this->session->userdata('username'); ?></p>
</div>

<div>
  <p><?php echo $data['username']; ?></p>
</div>

Is there any other way to echo a session?

  • 写回答

2条回答 默认 最新

  • dtt83024 2017-08-17 04:45
    关注

    1st : As per your comment you can return the usename as a response to ajax

            if($userdata) {
               echo json_encode(array('success'=>1,'username'=>$this->session->u‌​serdata('username'))‌​);
            } else {
               echo json_encode(array('success'=>0)‌​);
            }
    

    2nd : And update the DOM in ajax success like this .

      success:function(response){
    
                if(response.success==1){
                 $('#div_id').html(response.username);
               }else{
                  $('#div_id').html('error');
               }
        }
    

    3rd : And In your ajax dataType should be json

    dataType:'json'
    
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题