sess_destroy() 无法正常工作
sess_destroy() doesn't work properly
我正在尝试在 Codeigniter 中实现注销。这是我正在做的事情:
public function logout() {
if($this->session->userdata('session_id')){
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('session_id');
$this->session->unset_userdata('email');
$this->session->sess_destroy();
redirect('welcome', 'refresh');
}
}
这是登录功能:
public function authentication() {
$this->load->helper('html');
$this->load->library('javascript');
$this->load->helper('url');
$this->load->library('session');
$this->load->model('Tecnici_Model');
$this->load->library('encrypt');
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required');
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$this->db->where('e_mail', $email, TRUE);
$this->db->where('password', $password, TRUE);
$query = $this->db->get("tecnici");
$result = $query->num_rows();
if($result > 0) {
$newdata = array(
'session_id' => md5(uniqid(rand(), true)),
'email' => $email,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
redirect('welcome');
} else {
redirect('tecnici/login');
}
}
身份验证工作正常,因为 session_id 已设置,但是当我单击注销按钮时,它通过显示空白页面停止在注销方法中。我哪里做错了?谢谢!
更新 1
这是 Welcome 控制器的 index() 函数:
public function index() {
$this->load->helper('html');
$this->load->library('javascript');
$this->load->helper('url');
$this->load->library('session');
$this->load->view('header');
$this->load->view('welcome_message');
}
更新 2
我试过@AdrienXL 解决方案:
public function user_logout() {
$this->session->sess_destroy();
redirect('welcome');
}
并且我已经在 'welcome' 视图上打印了会话 ID。当我点击注销按钮时,会话 ID 每次都会更改。我无法解释为什么..
问题是,如果要销毁它,为什么要测试会话是否已设置?
此外,sess_destroy() 将销毁所有会话,甚至是闪存会话。您无需先取消设置。
一个简单的函数就足够了:
public function logout()
{
$this->session->sess_destroy();
redirect('welcome');
}
http://www.codeigniter.com/user_guide/libraries/sessions.html
编辑:
我觉得你太专注于 session_id。这是 CI 文档中的引述:
When a page is loaded, the session class will check to see if valid
session data exists in the user's session cookie. If sessions data
does not exist a new session will be created and saved.
这就是为什么你总是有 session_id。在你的情况下,你应该检查是否有一个名为 "logged_in" 的会话。
我正在尝试在 Codeigniter 中实现注销。这是我正在做的事情:
public function logout() {
if($this->session->userdata('session_id')){
$this->session->unset_userdata('logged_in');
$this->session->unset_userdata('session_id');
$this->session->unset_userdata('email');
$this->session->sess_destroy();
redirect('welcome', 'refresh');
}
}
这是登录功能:
public function authentication() {
$this->load->helper('html');
$this->load->library('javascript');
$this->load->helper('url');
$this->load->library('session');
$this->load->model('Tecnici_Model');
$this->load->library('encrypt');
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('password','Password','required');
$email = $this->input->post('email');
$password = md5($this->input->post('password'));
$email = mysql_real_escape_string($email);
$password = mysql_real_escape_string($password);
$this->db->where('e_mail', $email, TRUE);
$this->db->where('password', $password, TRUE);
$query = $this->db->get("tecnici");
$result = $query->num_rows();
if($result > 0) {
$newdata = array(
'session_id' => md5(uniqid(rand(), true)),
'email' => $email,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
redirect('welcome');
} else {
redirect('tecnici/login');
}
}
身份验证工作正常,因为 session_id 已设置,但是当我单击注销按钮时,它通过显示空白页面停止在注销方法中。我哪里做错了?谢谢!
更新 1
这是 Welcome 控制器的 index() 函数:
public function index() {
$this->load->helper('html');
$this->load->library('javascript');
$this->load->helper('url');
$this->load->library('session');
$this->load->view('header');
$this->load->view('welcome_message');
}
更新 2
我试过@AdrienXL 解决方案:
public function user_logout() {
$this->session->sess_destroy();
redirect('welcome');
}
并且我已经在 'welcome' 视图上打印了会话 ID。当我点击注销按钮时,会话 ID 每次都会更改。我无法解释为什么..
问题是,如果要销毁它,为什么要测试会话是否已设置? 此外,sess_destroy() 将销毁所有会话,甚至是闪存会话。您无需先取消设置。
一个简单的函数就足够了:
public function logout()
{
$this->session->sess_destroy();
redirect('welcome');
}
http://www.codeigniter.com/user_guide/libraries/sessions.html
编辑:
我觉得你太专注于 session_id。这是 CI 文档中的引述:
When a page is loaded, the session class will check to see if valid session data exists in the user's session cookie. If sessions data does not exist a new session will be created and saved.
这就是为什么你总是有 session_id。在你的情况下,你应该检查是否有一个名为 "logged_in" 的会话。