...

View Full Version : How to detect online/offline status



Mido™
09-12-2009, 07:44 PM
I hope to find any one could help me in knowing a way to detect the online/offline status of the users,I spent much time in it and I didn't reach the goal.

[vengeance]
09-12-2009, 08:06 PM
There are different ways.

You could either make a column in the database called "status" which changes from online to offline and vise versa automatically.

The way you could do that is to create a column called timestamp. And you probably have a config file - just update the user's timestamp and update the database.

Something like


$time = time() + 600;
$update = mysql_query("UPDATE `accounts` SET `timestamp` = '".$time."'") or die(mysql_error());

$setonline = mysql_query("UPDATE `accounts` SET `status` = 'online' WHERE `timestamp` > '".time()."'") or die(mysql_error());


Well, that was just an idea to get you started.

Eran
09-12-2009, 08:09 PM
it easy.
you need to manage a list (.txt\mySql) that contain the time the user last view the web page and to show the active users you request from the db all the active users within the last 5 minutes.

e.g
name | ip | last_active_time | start_time


when a user enters `page.php` you add him to the list.
+ 2 another things that you need to do is to update the the user "last_active_time" if he is not a new user.
+ and to delete old users that are more than y minutes or you can log this to archive but remember to keep this table small.
on the page you show all active users within the last x minutes.


btw, there are meny free scripts just google for it damn!!

Zangeel
09-12-2009, 11:40 PM
Store the sessions in the database:
http://www.developertutorials.com/tutorials/php/saving-php-session-data-database-050711/page1.html

Mido™
09-13-2009, 12:29 AM
;864344']There are different ways.

You could either make a column in the database called "status" which changes from online to offline and vise versa automatically.

The way you could do that is to create a column called timestamp. And you probably have a config file - just update the user's timestamp and update the database.

Something like


$time = time() + 600;
$update = mysql_query("UPDATE `accounts` SET `timestamp` = '".$time."'") or die(mysql_error());

$setonline = mysql_query("UPDATE `accounts` SET `status` = 'online' WHERE `timestamp` > '".time()."'") or die(mysql_error());


Well, that was just an idea to get you started.

I don't have a config file,also are you mean I should add a column in the users table or making a new table for this issue?

Mido™
09-13-2009, 12:31 AM
it easy.
you need to manage a list (.txt\mySql) that contain the time the user last view the web page and to show the active users you request from the db all the active users within the last 5 minutes.

e.g
name | ip | last_active_time | start_time


when a user enters `page.php` you add him to the list.
+ 2 another things that you need to do is to update the the user "last_active_time" if he is not a new user.
+ and to delete old users that are more than y minutes or you can log this to archive but remember to keep this table small.
on the page you show all active users within the last x minutes.


btw, there are meny free scripts just google for it damn!!

so how I can know or find the active users and which if this user is active or not?

Mido™
09-13-2009, 12:34 AM
Store the sessions in the database:
http://www.developertutorials.com/tutorials/php/saving-php-session-data-database-050711/page1.html

thanks but this site don't show how I can use this way in my problem.

[vengeance]
09-13-2009, 12:40 AM
Well, there are different ways, really. And I'm sure you'll find something on Google.

But the example I showed was how I made mine. I have a config file, which is included on the top of every page I have. That means the config file is loaded upon each refresh.

So if you run a script that posts the current time + 600 seconds (10 minutes) into the database, you can grab the timestamps later and do a simple "if timestamp is greater than current time" to check who's online.



$getOnline = mysql_query("SELECT `username` FROM `accounts` WHERE `timestamp` > '".time()."'") or die(mysql_error());
if(mysql_num_rows($getOnline) > 0){
while($online = mysql_fetch_array($getOnline)){
echo $online['username']."<br />";
}
}else{
echo "No one online.";
}


Hope you understand. I'm actually on the go, so I won't be replying from now on, but I'll get back to you later, if you need any further help.

Zangeel
09-13-2009, 12:40 AM
If you store user sessions in the database, you will know when a person signs on and signs off (i.e. find out of theyre online of offline)

Mido™
09-13-2009, 12:49 AM
If you store user sessions in the database, you will know when a person signs on and signs off (i.e. find out of theyre online of offline)

the idea look suitable but if you can explain the first steps of this way it will be better.

Mido™
09-13-2009, 11:24 PM
I added a new column and I make it value ="online" when the user log in and make it offline when he log off.
the code work well if only the user log off by the log off button,if he just closed the page and leave the site he is still online!
I need now just a way to detect if the user leaved my site or not.
note: I use the session and when user click log off the session destroyed.

[vengeance]
09-14-2009, 03:40 PM
the code work well if only the user log off by the log off button,if he just closed the page and leave the site he is still online!


That is why I suggested creating a timestamp column and update the timestamp for each individual user. After that you can get all users with a time() - 600 (10 minutes - your choice) or more and set those users' status to offline.

Mido™
09-14-2009, 09:35 PM
;864883']That is why I suggested creating a timestamp column and update the timestamp for each individual user. After that you can get all users with a time() - 600 (10 minutes - your choice) or more and set those users' status to offline.

I'll try this way,but the problem now if the user log in from a hour and during this time he is in my site he will be from the offline users.

[vengeance]
09-14-2009, 09:55 PM
I'll try this way,but the problem now if the user log in from a hour and during this time he is in my site he will be from the offline users.

I don't quite understand what you mean?

If you include a file on top of every page which runs a script, that updates the user's timestamp in the database, then he'll eventually go offline, if he's not active.

However when/if he refreshes a page the script will be executed again with a new timestamp, and the status will change to 'online'.

Mido™
09-14-2009, 11:27 PM
;865044']I don't quite understand what you mean?

If you include a file on top of every page which runs a script, that updates the user's timestamp in the database, then he'll eventually go offline, if he's not active.

However when/if he refreshes a page the script will be executed again with a new timestamp, and the status will change to 'online'.

Thanks but if you can put the that script or the things that should added in it I would be very thanked o you.

[vengeance]
09-14-2009, 11:49 PM
Thanks but if you can put the that script or the things that should added in it I would be very thanked o you.

It's common sense, really. I'll help you as much as I can, because I'll be going in a bit.

Create a new column in your users table called "timestamp".

Create a file called manage.php or whatever you desire. This'll be included on top of every page.

Be sure to change to the correct informations of yours.



<?php
session_start();

if(isset($_SESSION['user_logged_id'])) {
$setstamp = mysql_query("UPDATE `accounts` SET `timestamp` = '".time()."' WHERE `id` = '".$_SESSION['user_logged_id']."'") or die(mysql_error());
}
?>


And on top of every page



<?php
include("manage.php");
?>


To find online users/users that've been active in the past 10 minutes.



<?php
$time = time() - 600; // Within the past 10 minutes

$getOnline = mysql_query("SELECT `username` FROM `accounts` WHERE `timestamp` > '".$time."' ORDER BY `username` ASC") or die(mysql_error());
if(mysql_num_rows($getOnline) > 0){
while($online = mysql_fetch_array($getOnline)){
echo $online['username']."<br />";
}
}else{
echo "There are currently no users online.";
}
?>

Mido™
09-15-2009, 12:23 AM
Thank you very much for this help.:thumbsup:

jeremyb
06-02-2010, 04:36 AM
Vengeance

I have been searching the internet trying to get help on implementing this same process however I already have a login script that works fine. Therefore, I tried to add an additional timestamp field to my existing database and once the members authenticate I am having problems writing to that field. I can write to any other field. If you or anyone else would be available to give me a hand on this matter I would greatly appreciate it. My goal would be to get a list of all members who have an open session or checked for an open session in the last 5 minutes, based on the timestamp field.

Does anyone know of an easier method?

Thanks for any assistance.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum