Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Show when Users come online
Old 06-09-2005, 11:24 AM Show when Users come online
Monkeon's Avatar
Skilled Talker

Posts: 59
Trades: 0
Hi, I was just wondering if anyone knew of a script ( or how to make a script) that tells you when members of your website have logged on?

I have a site that uses a PHP login script using MySQL to authenticate the user's Username and Password.

I want to know how to show these members who is currently logged in.
For instance I would have a list of usernames running down the side of the page like this

Monkeon ONLINE
HippyDude
Joker ONLINE
JackTheLAd
Sirus2040


I thought that maybe I could include a little field in my MySQL table called 'Logon'
And when the user logs on, a value of 'ONLINE' could be inserted into the field.
The only problem is, how do I get that value to reset back to 'OFFLINE' if the user just closes the browser or something?

If anyone can tell me how to do it, or knows of a better way of doing it, I'd be very grateful!

Thanks
Monkeon is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-09-2005, 01:10 PM
Average Talker

Posts: 15
Location: New Jersey
Trades: 0
If you use sessions you can check who is online from the session variable. For example create a session variabel based on the username and password when they sign in. Then extract the username part of the session variable to indicate who is currently on your site. Then make it so the session has a timeout incase a person doesnt close there browser for a long time after the leave your site
wbarrett26 is offline
Reply With Quote
View Public Profile Visit wbarrett26's homepage!
 
Old 06-09-2005, 01:37 PM
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
OR.... you could have a table in your database where the user gets added to when they login and ... (somehow) deletes it from the table when they leave or logout. Just a thought.
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 06-09-2005, 02:16 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
ok.. here's the way i have it done on my CMS system (which keeps people logged in as well as user tracking). There is a table with user id, users sessison id, when they first logged on and when they were last seen.

When a user first logs in (first time ever, lets say) a session is started, and the session id is grabbed from there, and put into the database. At the same time, a cookie is set which only has two bit of numbered information - their userid (a number) and their session id - so the cookie means nothing to a hacker.

Each time a user navigates through the site, it updates the last seen field in the session table. It also check to see if they are online by matching the session number in their cookie with the session number in the table - this keeps them logged in.

I know i've very generally explained this, but it's because the whole system i a **** of a lot more complex than this. But simply, if i want to check who is online, i simply query the session table to see who has been active in the last, say, 5 minutes (the last seen field).

It is actually impossible, in PHP anyway, to check who is online in real time (i believe)
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 06-09-2005, 05:27 PM Thanks
Monkeon's Avatar
Skilled Talker

Posts: 59
Trades: 0
Thanks for the help guys.
I don't have a great deal of experience with sessions, but what you said made sense.

I'm guessing my login form and script should look something like this

FORM
HTML Code:
<form action="login.php" method="post">
<input name="loginuser" type="text" id="loginuser" size="15" />
<input name="loginpassword" type="password" id="loginpassword" size="15" />
<input type="submit" name="Submit" value="Submit" />
</form>
SCRIPT
PHP Code:
<?
session_start
();

// Check if the user is logged in
if(!isset($loginuser) | !isset($loginpassword)) {

// Show access denied page if the form wasn't sent
include "accessdenied.php";
exit();
}

// Form was sent. Authenticate the user

session_register("loginuser");
session_register("loginpassword");

// Check if the session login and password match with the MySQL data
include "authenticate.php";

// If login was successful, display member area

?>

So now my User is logged in with a session and lets assume my script also created a cookie with the member ID and session ID data stored inside it.

At the point of authentication I would need to Insert the Session ID into a MySQL table lets say it's called 'usersessions'.

which would look something like this

PHP Code:
mysql_connect($domain,$MySQLusername,$MySQLpassword);
@
mysql_select_db($database) or die( "Unable to select database");

$last_seen date('D M j G:i:s');

$query "INSERT INTO usersessions VALUES ('','$member_ID','$session_ID','$last_seen')";
mysql_query($query) or die( "Unable to write to User table");

mysql_close(); 
So...I would basically need this little piece of code on each of my pages, so that the user updates the timestamp in the 'Last Seen' field.

So far so good?
Monkeon is offline
Reply With Quote
View Public Profile
 
Old 06-10-2005, 07:14 AM
Monkeon's Avatar
Skilled Talker

Posts: 59
Trades: 0
One thing I don't get...

I need to tell my script to search for all users that logged on in the last 5 minutes.
So anything less than 5 minutes and the user is classed as being offline.

How do I get MySQL to search for last 5 minute records??

I probably need to rethink my timestamp, right?
Monkeon is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Show when Users come online
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17398 seconds with 12 queries