PHP implementation of user online status detection

Source: Internet
Author: User

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); }}}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.