Jump to content

users online and guests


doddsey_65

Recommended Posts

I am trying to display current online registered users and online guests. A user is defined as online by the online column in the database. if set to 1 they are online, if set to 0 they are offline.

 

so when a user logs in i set this column to 1 to show they are online. but when a user is inactive i set it to 0. i do this using:

 

if($user->last_active_time < time()-1800)
{
    $link->query("UPDATE ".TBL_PREFIX."users
                SET u_online = 0
                WHERE u_username = '".$user->user_name."'")
                or die(print_link_error());
}
else
{
    $link->query("UPDATE ".TBL_PREFIX."users
                SET u_online = 1
                WHERE u_username = '".$user->user_name."'")
                or die(print_link_error());
}

 

$user->last_active_time is set on every page load to the current time stamp(UNIX) however they are sometimes displayed as online or offline when they arent.

 

Also for displaying guests i use:

 

$session_time = time()-300;

$query = $link->query("SELECT g_ip FROM ".TBL_PREFIX."guests
                    WHERE g_ip = '".$_SERVER['REMOTE_ADDR']."'")
                    or die(print_link_error());

$num_rows = $query->rowCount();

if($num_rows == 0)
{
    if(!$user->user_name)
    {
        $link->query("INSERT INTO ".TBL_PREFIX."guests
                    (g_ip, g_time)
                    VALUES
                    ('".$_SERVER['REMOTE_ADDR']."', '".$config['time_now']."')")
                    or die(print_link_error());
    }
}

 

if the IP address of the current visitor is not in the database and they are not logged in then it adds their ip to the database.

 

but sometimes more guests are displayed then there actually are(tested this on local server so i know how many are online).

 

Are there any better ways i could be doing this?

 

Link to comment
Share on other sites

Hey, better or worse, this is what I'm using.  I have a separate table for users online aside from my "users" table.

$timeoutseconds = 30;
//get the time
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

//insert the values
$insert = mysql_query("INSERT INTO ".$conf['tbl']['useronline']." (timestamp, ip, level, userid) VALUES
('$timestamp','$_SERVER[REMOTE_ADDR]','$_SESSION[secure_level]','$newID')");
if(!($insert)) {
     print "Useronline Insert Failed > ";
}

//delete values when they leave
$delete = mysql_query("DELETE FROM ".$conf['tbl']['useronline']." WHERE timestamp<$timeout");
if(!($delete)) {
    print "Useronline Delete Failed > ";
}
//grab the results
$resultall = mysql_query("SELECT DISTINCT ip FROM ".$conf['tbl']['useronline']."");
if(!($resultall)) {
    print "Useronline Select Error > ";
}
$userall = mysql_num_rows($resultall);

Using the $userall query I go on to display total users.  I then query again for each level.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.