這篇文章主要介紹了基于PHP實(shí)現用戶(hù)在線(xiàn)狀態(tài)檢測,基于檢測文中通過(guò)示例代碼介紹的非常??詳細,對大家的現用學(xué)習或??者工作具有一定的參考學(xué)習價(jià)值,需要的朋友可(ke)以參考下
這個(gè)是基于ThinkPHP框架的,其他的戶(hù)線(xiàn)可以自己根據??需求改
1.先新建一個(gè)tags.p(O_O)hp文件,放在配置目錄Conf下。狀態(tài)
<?(⊙_⊙)基于檢測php
/*
* 添加行為
*
*/
return array(
'action_begin' => array('Onli??neCheck'),
);
?>
2.定義具體的功能
<?php
/*
* 定義行為: 在線(xiàn)更新
*/
class OnlineCheckBehavior extends Behavior {
//行為參數
protected $options = array(
'ONLINE_CHECK' => true, // 默認進(jìn)行在線(xiàn)
'ONLINE(???)_CHECK_TIME' => 10, // 默認5分鐘未活動(dòng),說(shuō)明已下線(xiàn)
);
public function run(&$params) {
if (C('ONLINE_CHECK')) {
// 更新ses??sion
if ((session('?現用login_account')) && (time() - session('access_time') > 60)) {
session('ac??cess_time', time(??));
}
// 在線(xiàn)更新
$ip = ip2long(get_cli(′?`*)ent_ip());
$online = M('Onl?ine');
// 先刪除在線(xiàn)表中 超過(guò)5分鐘未活??動(dòng)的記錄
//$sql = ' delete from __TABLE__?? where ';
$map['lasttime'] = array('??lt', time() - C('O??NLINE_CH??ECK_TIME') * 60);
$icount = $online->where($map)->delete();
if (session('?login_acc??ount'))(′?`*) { // 如果是登錄用戶(hù)
$map = ar??ray();
$map['uid'] = session('login_uid');
$map['las??tip'] = $ip;
$id = $online->where($map)->getField('id');
if (empty($id)) { // 不存在在線(xiàn)記錄,則清空session
session(null);
} else {
$map = array();
$map['id'] = array('eq',戶(hù)線(xiàn) $id);
$data['lasttime'] = time();
$data['lasti(╯°□°)╯︵ ┻━┻p'] = $ip;
$online->where($map)->save($dataヾ(?■_■)ノ);
}
} else { // 不是登錄用戶(hù) 游客
unset($map);
$map['lastip'] = array('eq', $ip);
$id = $online->where($map)->get??Field('??id');
//dump($id);
if (empty($id)) { // 不存在在線(xiàn)記錄, 則添加
$data = array();
$da??ta['uid'] = 0;
$data['account'] = 'Gu(′?`*)est';
$data['nickname'] = '游客';
$data['lasttime'] = time();
$data['lastip'] = $ip;
$online->add($d(???)ata??);
} else {
$map = array();
$map['id'] = array('eq',狀態(tài) $id);
$data['lasttime'] = time();
$data['lastip'] = $ip;
$online->where($map)->??;save($data);
}
}
}
}
}
?>
3.在具體的登錄方(fang)法上添加(?Д?)
// 登錄檢測
public function checkLogin() {
// $this->redirect($url);
$usernaヽ(′▽?zhuān)?ノme = strtolower($this->_param('usr'));
$pwd = $thi(′ω`*)s->_par??am('pwd');
$url = $this->_param('url'); // 目標地址
$is_error = false;
if (empty($username) or empty($pwd)) {
$this->assign('error_msg', '用戶(hù)名和口令不能為空');
$is_error = true;
}
if (!$is_error) {
$model = M('Usr');
$m(/ω\)ap['account'] = $username;
$map['upwd'] = strtoupper(md5($pwd));
$icount = $model->where($m┐(′д`)┌ap)->count();
if ($icount == 1) {
$list = $mode(′ω`*)l->??wh??ere($map)->find();
// 檢測用戶(hù)是否在線(xiàn)
if ($this->isOnline($list['id'])) {
// <editor-fold defaultstate="collapsed" desc="if開(kāi)始">
if ($list['status']) {
session('login_a??ccount',┐(′д`)┌ $userna(′Д` )me);
session('login_nickname', $list['nickname']);
session('last_time', toDate($list['last_time']));
if ($list['last_ip??']) {
session('last_ip', long2ip($(?????)list['last_ip']));??
} else {
session('last_ip', get_client_ip((′ω`)));
}
session('logi(′?ω?`)n_count', $list['login_count']);
sess(′_ゝ`)ion('login_uid', $list['id']);
session('login_pwd', $list['upwd']);
session('access_time', timˉ\_(ツ)_/ˉe()); //用戶(hù)最后點(diǎn)擊頁(yè)面時(shí)間 session超時(shí)使用
///
$map['id'] = $list['id'];
$data['l??ast_time'] = time();
$data['last_ip'] = ip2long(get_client_ip());
$model->where($map)->save($data);
$model->where($map)->setInc('login_count', 1);
// 檢測是否有同一IP的記錄,有更新,基(ji)于??檢測否則 添加
$online = M('Online');
$map = array();
$map['laヽ(′?`)ノstip'] = ip2long(get_client_i?p());
$online_id = $online->where($map)->getField('id');
if (empty($onli??ne_id)) {
// 插入在線(xiàn)用戶(hù)表
$data = array();
$data['uid'] = $list['id'];
$data['account'] = $list['account??'];
$data['nickname'] = $list['nickname'];
$data['lasttime'] = time();
$data['lastip'] = ip2long(get_client_ip());
$online-&??gt;add($data);
}else{
// 更新在線(xiàn)用戶(hù)表
$data = array();
$data['uid'] = $list['id'];
$data['account'] = $list['account'];
$data['nickname'] = $list['nickname'];
$data['la(′?ω?`)sttime'] = time();
//$data['lastip'] = ip2long(ge??t_client??_ip());
$online->where($map)->save($d(′_ゝ`)ata);
}
} else {
$is_error = true;
$this-??>as(′ω`)sign('error_msg',現用 '此用戶(hù)已被禁止登錄!');
}
// </editor-fold> if 結束
} else {
$is_error = true;
$th??is->assign('error_ms??g',戶(hù)線(xiàn) '此??用戶(hù)名已在其他電??腦登陸,請' . C('ONLINE_CHECK_TIME') .'分鐘后再試!');
}
} else {
$is_error = true;
$this->a(′▽?zhuān)?ssign('error_msg',狀(zhuang)態(tài) '錯誤的用戶(hù)名或口令!');
}
}
if ($is_error) {
$this->display('login');
} else {
$this->redirect('Index/index')??;
// if (empty($u(╯°□°)╯rl)) {
// $this->redirect('Index/index')(′_`);
// } else {
// $tヽ(′▽?zhuān)?ノhis->redirect($url);
// }
}
}
/**
* 檢測用(yong)戶(hù)是基于檢測否在線(xiàn)
* @access private
* @param int $uヽ(′?`)ノid 用戶(hù)ID
* @return Boolean true=不在線(xiàn)
*/
private function isOnline($uid) {
$ip = ip2long(get_client_ip());
$online = M('O(′;ω;`)nline');
$map['uid'] = array('eq', $u??id);
$list = $online->where($map)->find();
if (empty((T_T)$list)) { // 不存在
return true;
} else { // 存在,檢測IP是現用否一致,否則,戶(hù)線(xiàn)檢測是否超過(guò)5分鐘
if ($list['lastip'] == $ip) {
return true;
} else {
if ($list['lasttime'] < time() - C('ONLINE_CHECK_TIME') * 60) {
return true;
} else {
return false;
}
}
}
}
以上就是本文的全部?jì)热?,希望對大家的學(xué)習??有所幫助,也希望大家多多支持腳本之家。
來(lái)源:腳本之家