PHP Classes
elePHPant
Icontem

File: online.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Moazam  >  PHP Online Users  >  online.class.php  >  Download  
File: online.class.php
Role: Class source
Content type: text/plain
Description: Class responsible for tracking online users
Class: PHP Online Users
Keep track of site visitors
Author: By
Last change:
Date: 6 years ago
Size: 1,605 bytes
 

Contents

Class file image Download
<?php
class whoIsOnline {
   
    var
$time_to_remain_online;
    var
$max_time_online;
   
    function
whoIsOnline() { }
   
    function
setSessionTime($mins) {
       
$this->time_to_remain_online = time() + ($mins * 60);
       
$this->max_time_online = $mins * 60; //cleanup time
   
}
    function
isOnline($user_id) {
       
$check = false;
       
$a = mysql_query("SELECT user_name FROM `online` WHERE user_id=$user_id LIMIT 0,1");
        if(
$a) {
            if(
mysql_num_rows($a) > 0) {
               
$check = true;
            }
           
mysql_free_result($a);
        }
        return
$check;
    }
    function
recordUser() {
        global
$CURUSER;
       
//check if user is not guest
       
if(array_key_exists('user_id',$CURUSER)) {
           
//first time visit?
           
$tco = $this->time_to_remain_online;
            if(!isset(
$_SESSION['user_online']) || ($_SESSION['user_online'] - time()) <=0) {
                if(
$this->isOnline($CURUSER['user_id'])) {
                   
mysql_query("UPDATE `online` SET log_time=$tco WHERE user_id=$CURUSER[user_id]");
                }
                else {
                   
mysql_query("INSERT INTO `online` (`user_id`, `user_name`, `log_time`) VALUES($CURUSER[user_id],'$CURUSER[user]',$tco)");
                }
               
$_SESSION['user_online'] = $tco;
            }
        }
    }
    function
removeOnlineUser($user_id) {
       
mysql_query("DELETE FROM `online` WHERE user_id=$user_id");
        if(isset(
$_SESSION['user_online'])) {
            unset(
$_SESSION['user_online']);
        }
    }
   
//peform cleanup after certain period of time ... this function will be called by cron
   
function cleanup() {
       
$inactive = time()-$this->max_time_online;
       
       
$query = "DELETE FROM `online` WHERE `log_time` <= $inactive" ;
       
mysql_query($query);
    }
}
?>