除了在 var dump 中,Codeigniter 不会回显会话
Codeigniter doesn't echo session except in var dump
我有一个非常简单的 codeigniter 登录函数,它设置了 3 个会话数组,其中两个来自我的数据库。在我看来,我只能回应一个,但其余的都是作为对象出现的。我知道这可能是重复的,但我真的需要了解在这种情况下发生了什么。
我的观点
<input type="hidden" name="user_id" value="<?php echo $my_user->user_id; ?>">
型号
public 函数 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 中获取一个数组项以及从数据库
中获取另外 2 个数组项的方式
我的 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) 中有这个,我不知道如何克服,
能请教一下吗?
谢谢
在你的模型中试试这个:
return $this->db->get()->row()->id;
和
return $this->db->get()->row()->first_name;
我有一个非常简单的 codeigniter 登录函数,它设置了 3 个会话数组,其中两个来自我的数据库。在我看来,我只能回应一个,但其余的都是作为对象出现的。我知道这可能是重复的,但我真的需要了解在这种情况下发生了什么。
我的观点
<input type="hidden" name="user_id" value="<?php echo $my_user->user_id; ?>">
型号
public 函数 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 中获取一个数组项以及从数据库
中获取另外 2 个数组项的方式我的 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) 中有这个,我不知道如何克服,
能请教一下吗?
谢谢
在你的模型中试试这个:
return $this->db->get()->row()->id;
和
return $this->db->get()->row()->first_name;