【问题标题】:Unable to echo the username from a $_SESSION to the page无法将用户名从 $_SESSION 回显到页面
【发布时间】:2017-08-17 04:16:30
【问题描述】:
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>

还有其他方法可以回显会话吗?

【问题讨论】:

  • 我会 var_dump $this-&gt;session$this-&gt;session-&gt;userdata
  • 输出是什么?它只是空的吗?或者你得到一个错误?你$this-&gt;load-&gt;library('session');了吗?
  • 我这样做是为了检查是否设置了会话 Array echo "
    "; print_r($this->session->all_userdata());回声“
    ”;
  • 输出只是数组 ( [__ci_last_regenerate] => 1502943577 )
  • 我在配置中自动加载会话库

标签: php codeigniter session


【解决方案1】:

第一个:根据您的评论,您可以返回用户名作为对 ajax 的响应

        if($userdata) {
           echo json_encode(array('success'=>1,'username'=>$this->session->u‌​serdata('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');
           }
    }

3rd : 并且在你的 ajax 中 dataType 应该是 json

dataType:'json'

【讨论】:

  • @但需要 echo 1 才能知道用户已成功登录
  • 你可以像echo json_encode(array('success'=&gt;1,'username'=&gt;$this-&gt;session-&gt;userdata('username')));这样返回json数据并在成功函数中访问像这样console.log(response.success); console.log(response.username);@Lestah这样的值
  • 我明白了,所以我需要在我的 ajax 上添加 dataType: json 然后在我的控制台上查看它我需要执行 console.log(response.success) console.log(response.username) il试试这个
  • 你也可以看看我更新的答案@Lestah
  • 当我尝试控制台上的代码 Internal Server Error 让我仔细检查时得到了这个
【解决方案2】:

尝试像这样设置会话:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    相关资源
    最近更新 更多