当我的用户登录时,会更新last_active和last_visit宽度time()
。
用户登录任何访问页面后,会更新用户 last_active 和 last_visit 列。
我正在尝试找出最佳方式,因此即使在不同的浏览器中,如果用户在线或不在线,也可以显示用户个人资料页面。
http://sample.com/user/1
我似乎无法正确理解。当用户已注销时,它仍然在其他浏览器中将用户显示为在线
问题即使用户,我该如何检查用户是否在线? 不同的浏览器?
我看过How to see if a user is online in a website with php and mysql driven databases?
这些链接无济于事。
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;
}
模型
<?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();
}
}
}
控制器
<?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');
}
}
}