我有一个非常简单的codeigniter登录功能,它设置3个会话数组,其中两个来自我的数据库。我能够在我的视图中只回显一个,但休息来作为对象。我明白这可能是重复但我真的需要了解在这种情况下发生了什么。
我的观点
<input type="hidden" name="user_id" value="<?php echo $my_user->user_id; ?>">
模型
公共功能check_login($ email,$ password) {
$this->db->select('*');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get();
if ($query->num_rows() == 1){ return TRUE; } else { return FALSE; }
}
public function get_user_id($email, $password)
{
$this->db->select('id');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get();
return $query->row();
}
public function get_user_name($email, $password)
{
$this->db->select('first_name');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query = $this->db->get();
return $query->row();
}
控制器
public function verify_login()
{
if(isset($_POST['login']))
{
$name = $this->input->post('name');
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'password', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$data['form_red'] = "form-components-red.css";
$this->load->view('front/header', $data);
$this->load->view('front/signin');
$this->load->view('front/footer');
}
else
{
$password = md5($password);
$this->load->model('users_model');
$check_login = $this->users_model->check_login($email,$password);
if ($check_login == TRUE)
{
$get_user_id = $this->users_model->get_user_id($email, $password);
$get_user_name = $this->users_model->get_user_name($email, $password);
$session_array = array
(
'email' => $this->input->post('email'),
'user_id' => $get_user_id,
'name' => $get_user_name,
);
$this->session->set_userdata('logged_in', $session_array);
redirect ('/users/profile_menu');
}
else
{
$data['error_msg'] = '<div class="alert alert-danger">Login failed, please enter correct email or password</div>';
$data['form_red'] = "form-components-red.css";
$this->load->view('front/header', $data);
$this->load->view('front/signin', $data);
$this->load->view('front/footer');
}
}
}
else
{
$data['form_red'] = "form-components-red.css";
$this->load->view('front/header', $data);
$this->load->view('front/signin');
$this->load->view('front/footer');
}
}
public function update_profile()
{
// $data['user_data'] = $this->session->all_userdata();
$data['my_user'] = $this->session->userdata('logged_in');
$this->load->view('front/header-menu');
$this->load->view('front/update_profile',$data);
$this->load->view('front/footer');
}
正如您所看到的,这就是我如何从post获取一个数组项目,另外两个来自数据库
我的var转储看起来像这样
array(3) { ["email"]=> string(22) "me@aol.com" ["user_id"]=> object(stdClass)#15 (1) { ["id"]=> string(1) "1" } ["name"]=> object(stdClass)#16 (1) { ["first_name"]=> string(7) "Jacki" } }
所以我可以使用这种格式回复我的电子邮件
<input type="hidden" name="user_id" value="<?php echo $my_user->email; ?>">
但是当涉及到其他两个时,我在我的数组对象(stdClass)中有这个,我不明白如何克服,
你能建议吗?谢谢
答案 0 :(得分:0)
在你的模特中试试这个:
return $this->db->get()->row()->id;
和
return $this->db->get()->row()->first_name;