doushaizhen1244 2017-06-19 03:36
浏览 70

登录codeigniter后如何获取用户ID

I have successfully created a registration and login system. I have used username and password in the login form and i want to put the userid in other form like hidden field

login model cliente.php:

public function login($username,$password){
    $this->db->select('IdCliente');
    $this->db->where('LoginCliente',$username);
    $this->db->where('PassCliente',$password);
    $q = $this->db->get('clientes');

    if($q->num_rows()>0){
        return true;
    }else{
        return false;
    }

login controller login.php:

   public function index(){
    if($this->session->userdata('LoginCliente')){
        redirect('profile');
    }
    if(isset($_POST['password'])){
        $this->load->model('cliente');
        if($this->cliente->loginemp($_POST['username'],$_POST['password'])){
            $this->session->set_userdata('Login',$_POST['username']);
            redirect('PanelIndex');
        }elseif(isset($_POST['password'])){
        $this->load->model('cliente');
        if($this->cliente->login($_POST['username'],$_POST['password'])){
            $this->session->set_userdata('LoginCliente',$_POST['username']);
            $this->session->userdata('IdCliente');
            redirect('profile');
        }else{
            redirect('login');              
        }
    }

    }

    $this->load->view('inicio/loginview');  

}

profile.php

public function index()
{


         $this->load->view("/clientes/clienteindex");

}
  • 写回答

2条回答 默认 最新

  • douyu4535 2017-06-19 03:45
    关注

    If you want to IdCliente you need to return it first from the login function.

    public function login($username,$password){
        $this->db->select('IdCliente,LoginCliente');
        $this->db->where('LoginCliente',$username);
        $this->db->where('PassCliente',$password);
        $q = $this->db->get('clientes');
    
        if($q->num_rows()>0){
            return $q->row_array();
        }else{
            return false;
        }
    }
    

    Now In Index Function:

    $session_data = $this->cliente->login($_POST['username'],$_POST['password'])
    if(isset($session_data) && !empty($session_data){            
            $this->session->userdata('session_data',$session_data);
            redirect('profile');
     }
    

    At the Profile(at the View):

    $session_data = $this->session->userdata('session_data');
        $IdCliente= $session_data['IdCliente'];
        $LoginCliente= $session_data['LoginCliente'];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog