我试图弄清楚如何将用户名字添加到会话数据中。目前我只能添加电子邮件,甚至不知道我是怎么做到的。非常感谢任何帮助,谢谢。
这是我添加新用户的模型:
public function add_user($key){
$this->db->where('key', $key);
$temp_user = $this->db->get('temp_users');
if($temp_user){
$row = $temp_user->row();
$data = array(
'firstname' => $row->firstname,
'email' => $row->email,
'password' => $row->password
);
$did_add_user = $this->db->insert('users', $data);
}
if($did_add_user){
$this->db->where('key', $key);
$this->db->delete('temp_users');
return $data['email'];
} return false;
}
这是我的控制器信息(我当前正在为firstname的会话数据添加静态值&k;')我想添加从数据库中获取的实际用户名字:
public function register_user($key){
$this->load->model('model_users');
if($this->model_users->is_key_valid($key)){
if($newemail = $this->model_users->add_user($key)){
$data = array(
'firstname' => "kyle",
'email' => $newemail,
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('login/members');
}else echo "Failed to add user, please try again.";
}else echo "invalid Key";
}
以下是我的观点:
会话而> all_userdata();的print_r($ SESSION_INFO); ?>
以下是会话数组打印出来的内容:
Array ( [session_id] => 4fb01d348ba495ef010fe07415713167 [ip_address] => blah.blah.blah [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 [last_activity] => 1421942397 [user_data] => [firstname] => kyle [email] => freeride5567@yahoo.com [is_logged_in] => 1 )
答案 0 :(得分:2)
检查codeigniter文档:
https://www.codeigniter.com/user_guide/libraries/sessions.html
#1
public function add_user($key){
$this->db->where('key', $key);
$temp_user = $this->db->get('temp_users');
if($temp_user){
$row = $temp_user->row();
$data = array(
'firstname' => $row->firstname,
'email' => $row->email,
'password' => $row->password
);
$this->db->insert('users', $data);
if( $this->db->affected_rows() > 0 ){
$this->db->where('key', $key);
$this->db->delete('temp_users');
return (object)array(
'email' => $data['email'],
'firstname' => $data['firstname']
);
}
}
return false;
}
public function register_user($key){
$this->load->model('model_users');
if($this->model_users->is_key_valid($key)){
if($res = $this->model_users->add_user($key)){ //here
$data = array(
'firstname' => $res->firstname,
'email' => $res->email,
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('login/members');
}else echo "Failed to add user, please try again.";
}else echo "invalid Key";
}
答案 1 :(得分:1)
为了通过会话获得名字,tonoslfx的答案是正确的。
:将会话中的数据存储为数组:
return array(
'email' => $data['email'],
'firstname' => $data['firstname']
);
根据评论和聊天中的进一步讨论,以下是当用户在注册后登录时如何在会话中获取名字的解决方案:
在控制器中:
public function login_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'required|md5|trim');
$email = $this->input->post('email');
$password = $this->input->post('password');
if($this->form_validation->run()){
if($this->model_users->can_log_in($email, $password)) {
$userData = $this->model_users->getDataForSession($email);
$data = array(
"firstname" => $userData->firstname;
"email" => $email,
"is_logged_in" => 1
);
$this->session->set_userdata($data);
redirect('login/members');
}
}
else{
$this->signin();
}
}
在' model_users'模型,使用这个:
public function can_log_in($email, $password){
$this->db->where('email', $email);
$this->db->where('password', md5($password));
$query = $this->db->get('users');
if($query->num_rows() == 1){
return true;
}
else {
return false;
}
}
public function getDataForSession($email){
$this->db->select('firstname');
$this->db->where('email', $email);
return $this->db->get('users')->row();
}