【发布时间】: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->session->userdata('logged_in')[1] etc... -
这样的东西?
$userdata = array( 'id' => $id, 'email' => $email, 'name' => $name, 'logged_in' => TRUE );没用。 -
旁注:确保您再次“登录”,以便变量可以将它们“设置”到会话中。
标签: php codeigniter