Codeigniter sess_destroy () is not working properly, what am I doing wrong?

I am new to codeigniter. I use the login form as an administrator. When an administrator logs in with the correct username and password, he is redirected to the home page with a session variable. Then, if he presses the exit button, the session should be destroyed and redirected to the user on the login page i.e. get in shape.

The first controller is admin:

<?php class Admin extends CI_Controller { function index() { $data['main_content'] = 'admin/log_in'; $this -> load -> view('includes/admin/admin_template', $data); } function log_in() { $this->load->model('admin_model'); $query = $this -> admin_model -> validate(); if ($query)// if the user credentials validated... { $data = array('user_name' => $this -> input -> post('user_name'), 'is_logged_in' => true); $this -> session -> set_userdata($data); redirect('admin/home/admin_home'); } else// incorrect username or password { $this -> index(); } } function log_out() { $this->session->sess_destroy(); redirect('/admin/admin','refresh'); } } 

The second controller is the home controller:

 <?php class Home extends CI_Controller { function __construct() { parent:: __construct(); $this->is_logged_in(); } function is_logged_in() { $is_logged_in = $this -> session -> userdata('is_logged_in'); if (!isset($is_logged_in) || $is_logged_in != true) { $this -> load -> view('admin/forbidden'); } } function admin_home() { $data['main_content'] = 'home_view'; $this->load->view('admin/home_view'); } } 

Admin_model model:

 <?php class Admin_model extends CI_Model { function __construct() { parent:: __construct(); } function validate() { $this->db->where('user_name',$this->input->post('user_name')); $this->db->where('password', $this->input->post('password')); $query = $this->db->get('user'); if($query->num_rows==1) { return true; } } } 

Now he suggested that the user should log out and destroy the session, but if I click the back button of my browser, I can return the page back, which should not have been, and the session will not be destroyed. please tell me what I'm doing wrong here. I am using codeigniter 2.1.0.

+8
session-variables codeigniter-2
source share
2 answers

after going through all the problems and searching in different places, I finally found the right solution to this issue. The problem arose because the browser was showing cached pages. It was not the session that created the problem, and it was working properly. here is the solution: in the home controller adding the function to clear the cache and calling it in the constructor function does the trick :) here is the home controller with the solution:

 <?php class Home extends CI_Controller { function __construct() { parent:: __construct(); $this->is_logged_in(); $this->clear_cache(); } function is_logged_in() { if (!$this->session->userdata('is_logged_in')) { redirect('/admin/admin'); } } function clear_cache() { $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0"); $this->output->set_header("Pragma: no-cache"); } function admin_home() { $data['main_content'] = 'home_view'; $this->load->view('admin/home_view'); } } 

now thanks for this link " exit to the header of the code " link, this is where I found the solution, and it works great :)

+10
source share

If you log out, then although the session is destroyed, user session data remains throughout the current assembly of the CI page.

As a precaution, you should:

 function log_out() { $this->session->sess_destroy(); // null the session (just in case): $this->session->set_userdata(array('user_name' => '', 'is_logged_in' => '')); redirect('/admin/admin'); } 

See: http://codeigniter.com/forums/viewthread/110993/P130/#662369

+4
source share

All Articles