It is often necessary to detect the user's status when registering for registration. It's a special study today. But there should still be some small loopholes, first share to everyone, slowly improve
This is based on the thinkphp framework, others can be changed according to their own needs
1. First create a new tags.php file, placed in the configuration directory Conf.
<?php/*
Add behavior
*/return
Array (
' Action_begin ' => array (' Onlinecheck ')
;
? >
2. Define specific functions
<?php * * Definition behavior: Online update/class Onlinecheckbehavior extends Behavior {//behavioral parameter protected $options = Array (
' Online_check ' => true,//default online ' Online_check_time ' => 10,//default 5 minutes Inactive, description has been off-line; Public function run (& $params) {if (C (' Online_check ')) {//Update session if (Session ('? l
Ogin_account ') && (Time ()-session (' Access_time ') >)} {session (' Access_time ', Time ());
}//Online update $ip = Ip2long (Get_client_ip ());
$online = M (' online ');
First delete the inactive record in the online table for more than 5 minutes//$sql = ' Delete from __table__ where ';
$map [' lasttime '] = array (' LT ', Time ()-C (' online_check_time ') * 60);
$icount = $online->where ($map)->delete ();
if (Session ('? Login_account ')) {//If the logged-on user $map = Array ();
$map [' uid '] = session (' Login_uid ');
$map [' lastip '] = $ip; $id = $online->where ($map)->getfield (' id ');
if (empty ($id)) {//No online record exists, the session session (NULL) is emptied;
else {$map = array ();
$map [' id '] = array (' eq ', $id);
$data [' lasttime '] = time ();
$data [' lastip '] = $ip;
$online->where ($map)->save ($data);
} else {//not logged in user visitor unset ($map);
$map [' lastip '] = array (' eq ', $ip);
$id = $online->where ($map)->getfield (' id ');
Dump ($id);
if (empty ($id)) {//No online record exists, add $data = Array ();
$data [' uid '] = 0;
$data [' account '] = ' Guest ';
$data [' nickname '] = ' visitor ';
$data [' lasttime '] = time ();
$data [' lastip '] = $ip; $online->add ($data);
else {$map = array ();
$map [' id '] = array (' eq ', $id);
$data [' lasttime '] = time ();
$data [' lastip '] = $ip;
$online->where ($map)->save ($data); }}}}?>