> php教程 > php手册 > TP使用行为,实现在线用户检测

TP使用行为,实现在线用户检测

WBOY
풀어 주다: 2016-06-07 11:45:16
원래의
1336명이 탐색했습니다.

通过的TP的行为实现在线用户检测,实现同一个用户名仅允许一个在线。
tags.php文件,放在配置目录Conf下。<?php <br /> <br> /*<br>  * 添加行为<br>  *<br>  */<br> return array(<br>     'action_begin' => array('OnlineCheck'),<br> );<br> ?>行为定义:<?php <br /> <br> /*<br>  * 定义行为: 在线更新<br>  */<br> <br> class OnlineCheckBehavior extends Behavior {<br> <br>     //行为参数<br>     protected $options = array(<br>         'ONLINE_CHECK' => true, // 默认进行在线<br>         'ONLINE_CHECK_TIME' => 10, // 默认5分钟未活动,说明已下线<br>     );<br> <br>     public function run(&$params) {<br>         if (C('ONLINE_CHECK')) {<br>             // 更新session<br>             if ((session('?login_account')) && (time() - session('access_time') > 60)) {<br>                 session('access_time', time());<br>             }<br>             // 在线更新<br>             $ip = ip2long(get_client_ip());<br>             $online = M('Online');<br>             // 先删除在线表中 超过5分钟未活动的记录<br>             //$sql = ' delete from __TABLE__  where  ';<br>             $map['lasttime'] = array('lt', time() - C('ONLINE_CHECK_TIME') * 60);<br>             $icount = $online->where($map)->delete();<br>             if (session('?login_account')) { // 如果是登录用户<br>                 $map = array();<br>                 $map['uid'] = session('login_uid');<br>                 $map['lastip'] = $ip;<br>                 $id = $online->where($map)->getField('id');<br>                 if (empty($id)) { // 不存在在线记录,则清空session<br>                     session(null);<br>                 } else {<br>                     $map = array();<br>                     $map['id'] = array('eq', $id);<br>                     $data['lasttime'] = time();<br>                     $data['lastip'] = $ip;<br>                     $online->where($map)->save($data);<br>                 }<br>             } else { // 不是登录用户  游客<br>                 unset($map);<br>                 $map['lastip'] = array('eq', $ip);<br>                 $id = $online->where($map)->getField('id');<br>                 //dump($id);<br>                 if (empty($id)) { // 不存在在线记录, 则添加<br>                     $data = array();<br>                     $data['uid'] = 0;<br>                     $data['account'] = 'Guest';<br>                     $data['nickname'] = '游客';<br>                     $data['lasttime'] = time();<br>                     $data['lastip'] = $ip;<br>                     $online->add($data);<br>                 } else {<br>                     $map = array();<br>                     $map['id'] = array('eq', $id);<br>                     $data['lasttime'] = time();<br>                     $data['lastip'] = $ip;<br>                     $online->where($map)->save($data);<br>                 }<br>             }<br>         }<br>     }<br> <br> }<br> <br> ?>用户登录: // 登录检测<br>     public function checkLogin() {<br>         // $this->redirect($url);<br>         $username = strtolower($this->_param('usr'));<br>         $pwd = $this->_param('pwd');<br>         $url = $this->_param('url'); // 目标地址<br>         $is_error = false;<br>         if (empty($username) or empty($pwd)) {<br>             $this->assign('error_msg', '用户名和口令不能为空');<br>             $is_error = true;<br>         }<br>         if (!$is_error) {<br>             $model = M('Usr');<br>             $map['account'] = $username;<br>             $map['upwd'] = strtoupper(md5($pwd));<br>             $icount = $model->where($map)->count();<br>             if ($icount == 1) {<br>                 $list = $model->where($map)->find();<br>                 // 检测用户是否在线<br>                 if ($this->isOnline($list['id'])) {<br>                     // <editor-fold><br>                     if ($list['status']) {<br>                         session('login_account', $username);<br>                         session('login_nickname', $list['nickname']);<br>                         session('last_time', toDate($list['last_time']));<br>                         if ($list['last_ip']) {<br>                             session('last_ip', long2ip($list['last_ip']));<br>                         } else {<br>                             session('last_ip', get_client_ip());<br>                         }<br>                         session('login_count', $list['login_count']);<br>                         session('login_uid', $list['id']);<br>                         session('login_pwd', $list['upwd']);<br>                         session('access_time', time());  //用户最后点击页面时间  session超时使用<br>                         ///<br>                         $map['id'] = $list['id'];<br>                         $data['last_time'] = time();<br>                         $data['last_ip'] = ip2long(get_client_ip());<br>                         $model->where($map)->save($data);<br>                         $model->where($map)->setInc('login_count', 1);<br>                         // 检测是否有同一IP的记录,有更新,否则 添加<br>                         $online = M('Online');<br>                         $map = array();<br>                         $map['lastip'] = ip2long(get_client_ip());<br>                         $online_id = $online->where($map)->getField('id');<br>                         if (empty($online_id)) {<br>                             // 插入在线用户表<br>                             $data = array();<br>                             $data['uid'] = $list['id'];<br>                             $data['account'] = $list['account'];<br>                             $data['nickname'] = $list['nickname'];<br>                             $data['lasttime'] = time();<br>                             $data['lastip'] = ip2long(get_client_ip());<br>                             $online->add($data);<br>                         }else{<br>                              // 更新在线用户表<br>                             $data = array();<br>                             $data['uid'] = $list['id'];<br>                             $data['account'] = $list['account'];<br>                             $data['nickname'] = $list['nickname'];<br>                             $data['lasttime'] = time();<br>                             //$data['lastip'] = ip2long(get_client_ip());<br>                             $online->where($map)->save($data);<br>                         }<br>                     } else {<br>                         $is_error = true;<br>                         $this->assign('error_msg', '此用户已被禁止登录!');<br>                     }<br>                     // </editor-fold>   if 结束<br>                 } else {<br>                     $is_error = true;<br>                     $this->assign('error_msg', '此用户名已在其他电脑登陆,请' . C('ONLINE_CHECK_TIME') .'分钟后再试!');<br>                 }<br>             } else {<br>                 $is_error = true;<br>                 $this->assign('error_msg', '错误的用户名或口令!');<br>             }<br>         }<br>         if ($is_error) {<br>             $this->display('login');<br>         } else {<br>             $this->redirect('Index/index');<br> //            if (empty($url)) {<br> //                $this->redirect('Index/index');<br> //            } else {<br> //                $this->redirect($url);<br> //            }<br>         }<br>     }<br>   /**<br>      * 检测用户是否在线<br>      * @access private<br>      * @param int $uid 用户ID<br>      * @return Boolean true=不在线<br>      */<br>     private function isOnline($uid) {<br>         $ip = ip2long(get_client_ip());<br>         $online = M('Online');<br>         $map['uid'] = array('eq', $uid);<br>         $list = $online->where($map)->find();<br>         if (empty($list)) { // 不存在<br>             return true;<br>         } else { // 存在,检测IP是否一致,否则,检测是否超过5分钟<br>             if ($list['lastip'] == $ip) {<br>                 return true;<br>             } else {<br>                 if ($list['lasttime']                      return true;<br>                 } else {<br>                     return false;<br>                 }<br>             }<br>         }<br>     }

AD:真正免费,域名+虚机+企业邮箱=0元

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿