Unable to destroy session in encoder

What I want to implement is a simple login page if the user is successfully registered, and then redirected to the main page, and the login page still remains.

I have 1 controller named login, and 1 model named main . When the user clicks the login button, the name login / login_send is called.

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Login extends CI_Controller{ function __construct() { parent::__construct(); $this->load->model('model_login'); } function index() { if ($this->model_login->is_logged_in()) { redirect('main'); } else { // load login page $data['main'] = 'view_login'; $data['style'] = 'style_login'; $this->load->view('template', $data); } } function login_send() { $this->form_validation->set_rules('username', 'Username', 'trim|required'); $this->form_validation->set_rules('password', 'Password', 'trim|required'); if ($this->form_validation->run() == FALSE) { $this->index(); } else { if ( $this->model_login->validate_user() ) { $user_session_data = array( 'username' => $this->input->post('username'), 'is_logged_in' => 1 ); $this->session->set_userdata($user_session_data); redirect('main'); } else { redirect('login'); } } }// end function login_send function logout() { if ($this->model_login->is_logged_in()) { $this->session->sess_destroy(); $this->session->set_userdata(array('username' => '', 'is_logged_in' => 0)); log_message('debug', 'Some variable was correctly set'); } redirect('login','refresh'); } }// end class Login ?> 

Model_login here simply helps to check if the user is logged in by checking the session data.

 <?php class Model_login extends CI_MOdel{ function _isUserExist($username, $password) { $options = array( 'UserName' => $username, 'Password' => $password ); $query = $this->db->get_where('userinfo', $options); return $query->num_rows() > 0; } function validate_user() { $username = $this->input->post('username'); $password = $this->input->post('password'); return ($this->_isUserExist($username, $password)); } function is_logged_in() { $is_logged_in = $this->session->userdata('is_logged_in'); if ( !isset($is_logged_in) || $is_logged_in != 1 ) return FALSE; else return TRUE; } }// end class model_login ?> 

When you first log into the system, and then log in to the system, there are no problems. However, if I record a second time, I cannot log out. Even the login / logout was called correctly, I also updated the page, but session['is_logged_in'] == 1 . Is there something wrong with my code?

+4
source share
1 answer

In application/config/config.php try changing

 $config['sess_time_to_update'] = 300; //This is the default setting in ver 2.1.1 

to

 $config['sess_time_to_update'] = 0; 

This a few years ago caused me problems.

+9
source

Source: https://habr.com/ru/post/1394134/


All Articles