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

报告相同问题?

悬赏问题

  • ¥20 收一个快手协议下单算法
  • ¥15 求一个图片中的成交量选股公式
  • ¥15 已知正方形内随机生成坐标matlab
  • ¥30 关于#python#的问题:我想要的是这79个大特征对于房屋售价的最大的影响前十名(相关搜索:随机森林)
  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?
  • ¥15 mysql做碎片化处理老是报错怎么办
  • ¥15 如何正确在vs2010中初始化map对象
  • ¥30 mmdet3d模型部署问题
  • ¥15 comsol仿真反射率、吸收率时峰值位置和深度不对!
  • ¥30 Visual Studio找不到sdk,如何解决?