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.
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.
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');
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.