Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn through the TP behavior to achieve online user detection, so that the same user name can only be online.
Put the tags. php file in the configuration directory Conf.
/*
* Add Behavior
*
*/
Return array (
'Action _ begin' => array ('onlinecheck '),
);
?>
Behavior definition:
/*
* Define behavior: online update
*/
Class OnlineCheckBehavior extends Behavior {
// Behavior parameters
Protected $ options = array (
'Online _ check' => true, // ONLINE by default
'Online _ CHECK_TIME '=> 10, // No activity is performed for 5 minutes by default, indicating that the system has been deprecated.
);
Public function run (& $ params ){
If (C ('Online _ check ')){
// Update the session
If (session ('? Login_account ') & (time ()-session ('Access _ Time')> 60 )){
Session ('Access _ time', time ());
}
// Online update
$ Ip = ip2long (get_client_ip ());
$ Online = M ('Online ');
// Delete records that have not been active for more than 5 minutes in the online table
// $ 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 it is a logon user
$ Map = array ();
$ Map ['uid'] = session ('login _ uid ');
$ Map ['lastip'] = $ ip;
$ Id = $ online-> where ($ map)-> getField ('id ');
If (empty ($ id) {// if no online record exists, the session is cleared.
Session (null );
} Else {
$ Map = array ();
$ Map ['id'] = array ('eq ', $ id );
$ Data ['lasttime'] = time ();
$ Data ['lastip'] = $ ip;
$ Online-> where ($ map)-> save ($ data );
}
} Else {// not a logon user
Unset ($ map );
$ Map ['lastip'] = array ('eq ', $ ip );
$ Id = $ online-> where ($ map)-> getField ('id ');
// Dump ($ id );
If (empty ($ id) {// if no online record exists, add
$ Data = array ();
$ Data ['uid'] = 0;
$ Data ['account'] = 'guest ';
$ Data ['nickname'] = 'tourists ';
$ 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 );
}
}
}
}
}
?>
User Logon:
// Logon Detection
Public function checkLogin (){
// $ This-> redirect ($ url );
$ Username = strtolower ($ this-> _ param ('usr '));
$ Pwd = $ this-> _ param ('pwd ');
$ Url = $ this-> _ param ('url'); // target address
$ Is_error = false;
If (empty ($ username) or empty ($ pwd )){
$ This-> assign ('error _ msg ', 'user name and password cannot be blank ');
$ Is_error = true;
}
If (! $ Is_error ){
$ Model = M ('usr ');
$ Map ['account'] = $ username;
$ Map ['upwd '] = strtoupper (md5 ($ pwd ));
$ Icount = $ model-> where ($ map)-> count ();
If ($ icount = 1 ){
$ List = $ model-> where ($ map)-> find ();
// Check whether the user is online
If ($ this-> isOnline ($ list ['id']) {
//
If ($ list ['status']) {
Session ('login _ account', $ username );
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 ('login _ count', $ list ['login _ count']);
Session ('login _ uid', $ list ['id']);
Session ('login _ pwd', $ list ['upwd ']);
Session ('Access _ time', time (); // session timeout when the user finally clicks the page
///
$ Map ['id'] = $ list ['id'];
$ Data ['last _ time'] = time ();
$ Data ['last _ ip'] = ip2long (get_client_ip ());
$ Model-> where ($ map)-> save ($ data );
$ Model-> where ($ map)-> setInc ('login _ count', 1 );
// Check whether there are records with the same IP address and there are updates; otherwise, add
$ Online = M ('Online ');
$ Map = array ();
$ Map ['lastip'] = ip2long (get_client_ip ());
$ Online_id = $ online-> where ($ map)-> getField ('id ');
If (empty ($ online_id )){
// Insert an online user table
$ Data = array ();
$ Data ['uid'] = $ list ['id'];
$ Data ['account'] = $ list ['account'];
$ Data ['nickname'] = $ list ['nickname'];
$ Data ['lasttime'] = time ();
$ Data ['lastip'] = ip2long (get_client_ip ());
$ Online-> add ($ data );
} Else {
// Update the online user table
$ Data = array ();
$ Data ['uid'] = $ list ['id'];
$ Data ['account'] = $ list ['account'];
$ Data ['nickname'] = $ list ['nickname'];
$ Data ['lasttime'] = time ();
// $ Data ['lastip'] = ip2long (get_client_ip ());
$ Online-> where ($ map)-> save ($ data );
}
} Else {
$ Is_error = true;
$ This-> assign ('error _ msg ',' this user is not allowed to log on! ');
}
// If ends
} Else {
$ Is_error = true;
$ This-> assign ('error _ msg ',' this user name has been logged on to another computer. Please try again in '. C ('Online _ CHECK_TIME! ');
}
} Else {
$ Is_error = true;
$ This-> assign ('error _ msg ',' incorrect username or password! ');
}
}
If ($ is_error ){
$ This-> display ('login ');
} Else {
$ This-> redirect ('index/Index ');
// If (empty ($ url )){
// $ This-> redirect ('index/Index ');
//} Else {
// $ This-> redirect ($ url );
//}
}
}
/**
* Check whether the user is online
* @ Access private
* @ Param int $ uid user ID
* @ Return Boolean true = Not online
*/
Private function isOnline ($ uid ){
$ Ip = ip2long (get_client_ip ());
$ Online = M ('Online ');
$ Map ['uid'] = array ('eq ', $ uid );
$ List = $ online-> where ($ map)-> find ();
If (empty ($ list) {// does not exist
Return true;
} Else {// yes. Check whether the IP address is consistent. Otherwise, check whether the IP address exceeds 5 minutes.
If ($ list ['lastip'] = $ ip ){
Return true;
} Else {
If ($ list ['lasttime'] <time ()-C ('Online _ CHECK_TIME ') * 60 ){
Return true;
} Else {
Return false;
}
}
}
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB