On my Auth library I need it to update my active column on my dataabase when session expire.
Currently if session expired and refreshes page it triggers my $this->auth->logout() function
But does not update the users active column to 0? It unsets user id fine and token fine but not update active column.
Question: When the session has expired how can I make sure it updates the users active column on database to 0
Library
Auth.php (Size: 2.04 KB / Downloads: 0)
Currently if session expired and refreshes page it triggers my $this->auth->logout() function
But does not update the users active column to 0? It unsets user id fine and token fine but not update active column.
Question: When the session has expired how can I make sure it updates the users active column on database to 0
PHP Code:
public function logout() {
$udata = array(
'active' => '0',
);
$this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
$this->CI->db->update($this->CI->db->dbprefix . 'user_data', $udata);
unset($_SESSION['user_id']);
unset($_SESSION['token']);
$this->user_id = '';
$this->username = '';
}
Library
PHP Code:
<?php
class Auth {
private $user_id;
private $CI;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->helper('string');
if ($this->CI->session->userdata('user_id')) {
$this->CI->db->select('ud.*, u.username');
$this->CI->db->from($this->CI->db->dbprefix . 'user u');
$this->CI->db->join($this->CI->db->dbprefix . 'user_data ud', 'ud.user_id = u.user_id');
$this->CI->db->where('u.user_id', $this->CI->session->userdata('user_id'));
$query = $this->CI->db->get();
if ($query->num_rows() == 1) {
$this->user_id = $query->row()->user_id;
$this->username = $query->row()->username;
$udata = array(
'last_loggedon' => time(),
'active' => '1',
'ip_address' => $this->CI->input->ip_address()
);
$this->CI->db->where('user_id', $query->row()->user_id)->update($this->CI->db->dbprefix . 'user_data', $udata);
} else {
$this->logout();
}
}
}
public function login($username = NULL) {
$this->CI->db->select('ud.*, u.username');
$this->CI->db->from($this->CI->db->dbprefix . 'user u');
$this->CI->db->join($this->CI->db->dbprefix . 'user_data ud', 'ud.user_id = u.user_id');
$this->CI->db->where('u.username', $username);
$query = $this->CI->db->get();
if ($query->num_rows() == 1) {
$this->CI->session->set_userdata(array('user_id' => $query->row()->user_id, 'token' => random_string('alnum', 32)));
$this->user_id = $query->row()->user_id;
$this->username = $query->row()->username;
return TRUE;
} else {
return FALSE;
}
}
public function islogged() {
return $this->user_id;
}
public function logout() {
$udata = array(
'active' => '0',
);
$this->CI->db->where('user_id', $this->CI->session->userdata('user_id'));
$this->CI->db->update($this->CI->db->dbprefix . 'user_data', $udata);
unset($_SESSION['user_id']);
unset($_SESSION['token']);
$this->user_id = '';
$this->username = '';
}
}
Auth.php (Size: 2.04 KB / Downloads: 0)