【问题标题】:How to access all userdata via session in codeigniter如何通过codeigniter中的会话访问所有用户数据
【发布时间】:2016-10-06 12:50:33
【问题描述】:

请我创建了一个登录并传递了电子邮件和 ID,我可以在我的视图中访问它们。但我无法访问其他用户数据,如姓名、电话等。我只能访问电子邮件、ID 和会话 ID。感谢您的帮助。

这是我的模型

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;
    }

}

这是我的控制器

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');

}

【问题讨论】:

  • id, email, logged_in 是您设置的唯一会话变量。
  • 我添加了其他变量,当我尝试从视图中获取它们时,它为空。您能否向我建议一种设置其他变量的方法?
  • 向您的$userdata 添加其他索引。然后您可以访问$this-&gt;session-&gt;userdata('logged_in')[1] etc...
  • 这样的东西? $userdata = array( 'id' =&gt; $id, 'email' =&gt; $email, 'name' =&gt; $name, 'logged_in' =&gt; TRUE ); 没用。
  • 旁注:确保您再次“登录”,以便变量可以将它们“设置”到会话中。

标签: php codeigniter


【解决方案1】:

您可以在模型中设置会话

参考下面的代码。查看如何在会话中设置其他变量

型号

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;
    }
  }

您可以在任何地方调用该会话变量。

【讨论】:

  • 那么如何使用这个模型函数调用视图中的变量呢?因为$row-&gt;name 返回未定义变量错误。
  • 感谢@robins,我能够调整我的控制器以适应模型的变化。现在一切都很好。 Codeigniter(一般编程)可能很奇怪。
猜你喜欢
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
相关资源
最近更新 更多