Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-11-2003, 02:42 PM   #1 (permalink)
Antagony
Registered User
 
Antagony's Avatar
 
Join Date: Mar 2003
Posts: 31
Antagony is on a distinguished road
Send a message via ICQ to Antagony
Online/Offline Status?

I'm coding a site that uses mySQL, and I'm trying to figure out a way to determine whether a user is online or offline.

Obviously, when they log in, that will mark them as online. And when they log out, they will be offline.

BUT, the problem is with people wandering away from their computer or whatever. So I need a way for them to "time out".

How would you go about doing this?
Antagony is offline   Reply With Quote
Old 06-11-2003, 03:45 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
make a field in your the user table named "lastvisit"

update that field with the curent time everytime the user clicks a page while logged in.

then figure out your timeout time ... lets say five minutes .. and you can query against their last visit to see if they are still logged in.
__________________
Mike
sde is offline   Reply With Quote
Old 06-11-2003, 03:47 PM   #3 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
You don't say whether or not you're using PHP, but we'll assume so. What about setting a cookie that times out after an hour or whatever value you feel is appropriate, then if they try to access the site functions after that time, no dice.
bdl is offline   Reply With Quote
Old 06-11-2003, 04:05 PM   #4 (permalink)
Antagony
Registered User
 
Antagony's Avatar
 
Join Date: Mar 2003
Posts: 31
Antagony is on a distinguished road
Send a message via ICQ to Antagony
Okay, understandable solutions, but here's another small catch --

The site has low activity. By that, I mean that there is very little for the user to do, so refreshing a timestamp isn't really the best solution.

As for making a cookie time out, I know how to do that, but how does the site know when that cookie has timed out?
Antagony is offline   Reply With Quote
Old 06-11-2003, 09:23 PM   #5 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally posted by Antagony
As for making a cookie time out, I know how to do that, but how does the site know when that cookie has timed out?
I suppose you would control the login with sessions. Once the cookie times out, the next page the user tries to access looks for that cookie. If it doesn't exist, delete the session, log the user out before they try to access the next page.
bdl is offline   Reply With Quote
Old 06-11-2003, 09:46 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
Re: Online/Offline Status?

Quote:
Originally posted by Antagony
I'm coding a site that uses mySQL, and I'm trying to figure out a way to determine whether a user is online or offline.
i am user "sde" .. you are user "joe" there is no way i can determine whether you are online or not by the cookie on your machine unless the cookie information was writting to a place where i can access information from too .. ( like the database )

here's a general idea of how to database a unix timestamp and then check to see what users are logged in:
PHP Code:
<?
// insert something like this at the top of each page
// the 'lastvisit' field in the db is an int(14)
$now=date();

// this assumes that $user_id is the unique identifier
// and exists in this php script already
$result=mysql_query("update users set lastvisit='$now' where user_id='$user_id'");
?>
that will update the database with a unix timestamp everytime the user goes to a new page ..

so the code to check what users are still logged will be determined by who has a lastvisit later than 5 minutes ago.
PHP Code:
<?
$five_minutes_ago
=date()-300;

$result=mysql_query("select user_name from users where lastvisit > '$five_minutes_ago'");
?>
i'm sure there's other methods, but i think this might be easiest.
__________________
Mike
sde is offline   Reply With Quote
Old 06-12-2003, 01:24 PM   #7 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Hmm. I guess cookies don't just disappear after expiration times as I initially thought; the other idea I had would be to actually make the time() as the value of your cookie, then check the current time() against the value of your cookie, if it extends over a period of time, log the user out. I tried a little test script to do this, and it works in a very limited way. Might be something to think about anyway.

bdl is offline   Reply With Quote
Old 06-12-2003, 01:52 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
if you're using cookies though .. you're the only one that can see your online/offline status. which is useless because you, the user knows if you are online or offline.

i guess i'm not understanding the logic.

it seems like he wan't to see other people's online/offline status which means a database would have to be involved if he wants to reference online users with their usernames.
__________________
Mike
sde is offline   Reply With Quote
Old 06-14-2003, 05:22 AM   #9 (permalink)
Antagony
Registered User
 
Antagony's Avatar
 
Join Date: Mar 2003
Posts: 31
Antagony is on a distinguished road
Send a message via ICQ to Antagony
Thanks for the tips.

I'm going to work on the code some today, so if I decide on how to do this I'll post again and tell you guys what I did :rock:
Antagony is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC status update sde Code Newbie News 0 01-09-2004 06:45 AM


All times are GMT -8. The time now is 06:54 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting