donkey199024 2016-06-06 11:30
浏览 42
已采纳

如何通过codeigniter中的会话访问所有userdata

Please I have created a login and passed email and id and I can access them in my view. But I am unable to access other userdata like name, phone, etc. Am only able to access email, id and session id. Your help is appreciated.

here is my model

public function login($email, $password){
    $this->db->select('*');
    $this->db->from($this->table);
    $this->db->where('email', $email);
    $this->db->where('passwd', $password);
    $this->db->limit(1);

    $query = $this->db->get();

    if ($query->num_rows() == 1) {
        return $query->row()->uid;
    }
    else {
        return FALSE;
    }

}

here is my controller

public function login(){

    $this->form_validation->set_rules('email', 'Email', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');


if($this->form_validation->run() == FALSE) {

    echo validation_errors('<p class="alert alert-dismissable alert-danger">');
}
else{

        //Get post data
        $email = $this->input->post('email');
        $password = $this->input->post('password');

        $enc_pwd = sha1($password);
        $id = $this->User_model->login($email, $enc_pwd);

        if($id){
            $userdata = array(
                'id'        =>  $id,
                'email'     =>  $email,
                'logged_in' =>  TRUE
                );


            //Set session data
            $this->session->set_userdata($userdata);


            //Redirect to Users page
            redirect('site/dashboard');
        }
        else{
            //Create Error
            $this->session->set_flashdata('error', 'Login credentials are incorrect');


            //Redirect to Users page
            redirect('site/login');
        }

        //Add activity
        //$this->Activity_model->add($data);            
    }

    //Load the login template
    $this->load->view('public/login');

}

展开全部

  • 写回答

1条回答 默认 最新

  • doushui3216 2016-06-07 04:33
    关注

    You can set your sesssion in the Model

    Refer the code below.Check how to set other variables in session

    Model

    function login($email, $password)
    {
        $this -> db -> select('*');
        $this -> db -> from($this->table);
        $this->db->where('email', $email);
        $this->db->where('passwd', $password);
        $this->db->limit(1);
        $query = $this -> db -> get();  
        if($query -> num_rows() == 1)
        {
            $row = $query->row();
            $data = array(
            'email'=>  $email,
            'id' => $row->id,
            'name' => $row->name,
            'phone' => $row->phone,
            'logged_in' =>  TRUE 
            );
            $this->session->set_userdata($data);
            return true;
    
        }
        else
        {
            return false;
        }
      }
    

    Anywhere you can call that session variable.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部