When my user logs in it updates the last_active and last_visit width time()
.
Once user has logged on any page that is visits it updates the users last_active and last_visit column.
I am trying to figure out the best way so on the users profile page it can show if user is online or not even if in different browser.
http://sample.com/user/1
I can not seem to get it correct. It still shows user as online in other browsers when infact the user has logged out
Question how can I check if the user is online or not even if in a different browser?
I have looked at How to see if a user is online in a website with php and mysql driven databases?
And PHP Check if a user is online
The links not help.
if(date("H:i:s", $udata['last_active']) == date("H:i:s", $udata['last_visit']) && date("H:i:s", $udata['last_active']) != date("H:i:s", strtotime("+1 min"))) {
$this->data['active'] = 1;
} else {
$this->data['active'] = 0;
}
Model
<?php
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
if ($this->islogged() == 1) {
$this->db->where('user_id', $this->getuserid());
$query = $this->db->get($this->db->dbprefix . 'user_data');
if ($query->num_rows() == 1) {
$user_data = array(
'last_active' => time(),
'last_visit' => time(),
'ip_address' => $this->input->ip_address()
);
$this->db->where('user_id', $query->row()->user_id);
$this->db->update($this->db->dbprefix . 'user_data', $user_data);
}
} else {
$this->logout();
}
}
}
Controller
<?php
class User extends MY_Controller {
public $data;
public function __construct() {
parent::__construct();
$this->load->library('elapsed_time');
$this->load->model('forum/thread_model');
$this->load->model('user/user_model');
}
public function index($user_id) {
$udata = $this->user_model->getuser($user_id);
if ($udata) {
// Titles
$this->data['title'] = 'Profile of ' . $udata['username'];
$this->data['heading_title'] = 'Forum Info';
// Page Data
$this->data['profile_username'] = $udata['username'];
if(date("H:i:s", $udata['last_active']) == date("H:i:s", $udata['last_visit']) && date("H:i:s", $udata['last_active']) != date("H:i:s", strtotime("+1 min"))) {
$this->data['active'] = 1;
} else {
$this->data['active'] = 0;
}
$this->data['how_long_was_online'] = 'Created account but not logged in yet!';
$this->data['date_created'] = date('d-m-Y h:i:s' , $udata['date_created']);
$this->data['avatar'] = ($udata['avatar']) ? $udata['avatar'] : 'holder.js/100px180';
$this->data['total_questions'] = $this->thread_model->total_users_thread_questions($user_id);
$this->data['total_threads'] = $this->thread_model->total_users_threads($user_id);
// Page Links
$this->data['users_questions'] = site_url('search/questions/' . $user_id);
$this->data['users_threads'] = site_url('search/threads/' . $user_id);
$this->data['pm'] = site_url('mail/pm/' . $user_id);
$this->data['email'] = site_url('mail/email/' . $user_id);
$this->data['children'] = array(
'common/header',
'common/footer',
'common/navbar'
);
$this->load->render('user/user', $this->data);
} else {
redirect('not_found');
}
}
}