Jump to content

Simple 'Who's online?' List


LemonInflux

Recommended Posts

Ok, this is half logic, half code help. Basically, I can figure out how to add people to the list. I'd presume you'd have a column in a database table called on/off. In them, you could either have 1 or 0. If someone's online, 1, if not, 0. I'd guess that the 1 would be written as they log in and the sessions are registered. But, how do you write the 0? At what point is that written? So basically, how would you do it, and code if possible? Thanks in advance.

Link to comment
Share on other sites

The only problem with your method is you can't rely on the user to logout. The logout page would be where you set it to 0...but thats not going to work as most people just go to the next website without logging out.

 

What you have to do is make a column in the users table called "last_active" or something like that. Every time they click, you need to update that column with a timestamp. Then, to display the online list, your query would look like this:

 

SELECT username FROM users WHERE last_active > (NOW() - INTERVAL 5 MINUTE) ORDER by userID ASC

 

That will grab everyone who has clicked on your webste in the last 5 minutes (the users logged in at least)

Hopefully that makes sense.

Link to comment
Share on other sites

Call this code on every page...so if you have a header page, put it there.

<?php

$query = mysql_query("UPDATE users SET last_active=NOW() WHERE userID='$userID'")or die(mysql_error());

?>

 

Now put this on the page where you want to display the users online

<?php

$query = "SELECT username FROM users WHERE last_active > (NOW() - INTERVAL 5 MINUTE)";
$result = mysql_query($query)or die(mysql_error());

while ($row = mysql_fetch_assoc($result)){
   echo $row['username'].'<br>';
}

?>

 

Obviously your going to have to change up the queries to match your database.

 

 

Link to comment
Share on other sites

I agree with pocobueno1388 way of going about this, but if your having trouble doing that. Then you could simply do the way you suggested the on/off field in the database. They login, 1 is inserted into the database. Logout page, update it to 0. And each page you can check if the session variables exist. If they don't update it to 0. And thats not to say, a better way to do this then that, would be. When they log in, create a session variable for their user id.

and another session variable for on/off , then if they close their browser or dont logoff, you can time them out. And you will still be able to work with the database cause you have their id.

 

I hope this helps.  :)

 

Regards ACE

 

 

Link to comment
Share on other sites

i do basically the same as pocobueno... except i don't use the datetime, i do it like this...

 

the database would have these 2 fields, online and last_active (last_active is a varchar)

 

then on the login page you would have this:

mysql_query("UPDATE `users` SET `online`='1', `last_active`='".time()."' WHERE `username`='{$username}'");

 

all that does is sets the user to online and sets the last_active to the current time.

 

now you need a page that will be included on every page, so that when a user access a page the included script is run..

so in the included script you'd have this:

 

$session_timeout = 5; //in minutes
$timeout = time() - ($session_timeout * 60); //if any users with a timeout equal to or less then this number, they get signed out.

if(isset($_SESSION['member_session'])){ //check if your member login session is running, if it is, a member is logged in.

$last_update = mysql_result(mysql_query("SELECT `last_active` FROM `users` WHERE `username`='{$username}'"),0); //grab the last update for this user.

if($last_update <= $timeout){ //check the update, if its less then the timeout then we sign them out.

mysql_query("UPDATE `users` SET `online`='0', `last_update`='' WHERE `username`='{$username}'");
session_destroy(); //sign the user out

}else{ //if its not, we update there last_update to the current time
mysql_query("UPDATE `users` SET `last_update`='".time()."' WHERE `username`='{$username}'");
}

}

 

first this script creates the variables for timing out. then it checks if the user is online, you can add an else statement to this to add in for guests if you want. Then it grabs the users last update from the table and checks it against the timeout variable. If the if statement passes, it sets the users online value to 0 and destroys the session. if the statement fails, it updates the database setting the last_update to the current time.

 

hope this gets you motivated! :D

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.