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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?