-1

I use Codeigniter 3 and database as a session driver. Everything is working fine, but the session records are not deleted from database. Should I delete them manually or..?

Waiting for your answer.

Thank you.

Vesna
  • 37
  • 4
  • 1
    What you have tryed ? Provide the codes in your question – Praveen Dabral Apr 28 '15 at 11:41
  • I don't have any code for deleting records from database. In CI 2 it says: The Session class has built-in garbage collection which clears out expired sessions so you do not need to write your own routine to do it. But I'm using CI3 and it seems that sessions from database are not deleted. – Vesna Apr 28 '15 at 12:32

2 Answers2

2

You means when user logout you want to clear the session. If u means that you can use the code below. You must remove the data inside session manually.If you not destroy it manually, then the data will be available in your databas until it reach session expiration.

session_destroy();    
// or    
$this->session->sess_destroy();

Or if you want to clear spesific data inside session you can use the code below

unset($_SESSION['some_name']);
// or
$this->session->unset_userdata('some_name');

If you use session to store flash data, then the session only available only for next request. Example code like below

$_SESSION['item'] = 'value';
$this->session->mark_as_flash('item');
Faiz
  • 1,021
  • 9
  • 10
  • Thank you for your answer. Session expiration is reached but the records from database are not deleted. – Vesna Apr 28 '15 at 12:29
1

Copied from here

Create a new column in the ci_session table.

ALTER TABLE `yourdatabase`.`ci_sessions` 
ADD COLUMN `userid` VARCHAR(45) NULL  AFTER `user_data` ;

Then in your login function get the id from your login process and before adding the userdata information to the session do:

//delete any existing sessions with the current userid session.
// Note - a new session has already been generated but doesn't have a value
// in the userid column, so won't get deleted.
$this->db->delete('ci_sessions',array('userid' => $identity));    

//get the current session and update it with the userid value.
$session_id = $this->session->userdata('session_id');
$this->db->where('session_id', $session_id);
$this->db->update('ci_sessions', array('userid' => $identity));

Where $identity is your userid. That clears any previous sessions and means only one exists at a time.

Community
  • 1
  • 1
Praveen Dabral
  • 2,449
  • 4
  • 32
  • 46