duanhuayong6687 2019-04-29 00:51
浏览 63
已采纳

无法从CodeIgniter中的会话登录中获取和显示数据名称

So i wanna make some login page and then show the user name in the dashboard after login succeed. But the name won't show in the dashboard. its just blanks. there's no error notification or something. please help. thanks

I'm running on PHP 7


Auth.php Controller

public function index()
{
    $this->form_validation->set_rules('email','email',
        'trim|required|valid_email');
    $this->form_validation->set_rules('password','Password','trim|required');
    if($this->form_validation->run() == false){
        $data['title']='Login Page';
        $this->load->view('templates/auth_header', $data);
        $this->load->view('auth/login');
        $this->load->view('templates/auth_footer');
    }
    else{
        //success validation
        $this->_login();


    }

}

private function _login()
{
    $email = $this->input->post('email');
    $password = $this->input->post('password');

    $user = $this->db->get_where('user',['email' => $email])->row_array();

    if($user){
        //user active
        if($user['is_active']==1){
            //check password
            if(password_verify($password, $user['password'])){
                //if pass were right
                $data = [
                    'email' => $user['email'],
                    'role_id' => $user['role_id']
                ];
                $this->session->set_userdata('$data');
                redirect('user');

            }
            else{
                $this->session->set_flashdata('message','<div class="alert alert-danger" role="alert"> Wrong password</div>');
                redirect('auth');
            }

        }
        else{
        //
        $this->session->set_flashdata('message','<div class="alert alert-danger" role="alert"> 
            Email has not been activated</div>');
        redirect('auth');

    }

    }
    else{
        //no email in database
        $this->session->set_flashdata('message','<div class="alert alert-danger" role="alert"> 
            Email is not registered</div>');
        redirect('auth');

    }

}

User.php in controller (control for user)

public function index()
{

    $data['user'] = $this->db->get_where('user',['email' => $this->session->userdata('email')])->row_array();
    $data['title']='Dashboard - SmartStock';
    $this->load->view('user/index', $data);
}

and command for showing the data in page

index.php

  <div class="navbar-menu-wrapper d-flex align-items-stretch">

    <ul class="navbar-nav navbar-nav-right">
      <li class="nav-item nav-profile dropdown">
        <a class="nav-link dropdown-toggle" id="profileDropdown" href="#" data-toggle="dropdown" aria-expanded="false">
          <div class="nav-profile-img">
            <img src="<?= base_url('assets/'); ?>images/profile/default.png" alt="image">
            <span class="availability-status online"></span>             
          </div>
          <div class="nav-profile-text">
            <p class="mb-1 text-black"><?= $user['name']; ?></p>
          </div>
        </a>

  • 写回答

1条回答 默认 最新

  • duanlian1320 2019-04-29 03:04
    关注

    Perhaps your mistake is Here in Auth->_login() line set_userdata,
    replace

    $this->session->set_userdata('$data');
    

    with

    $this->session->set_userdata($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡