Online / Offline status

Building an Intranet system

Page 1 of 1

8 Replies - 3690 Views - Last Post: 05 November 2008 - 04:01 AM Rate Topic: -----

#1 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Online / Offline status

Post icon  Posted 03 November 2008 - 04:08 AM

Hows it going guys?

The brief:

I've built (or am in the process of building) an Intranet system for my company, and everything works as it should so thumbs up for stage one, however...

Each user has an online / offline status, which at the moment is a BOOL column in the database, set to 1 when a user logs on and 0 when they log off - this obviously isn't ideal, but i wanted to skim past this feature until the final stage, which is where I am now.

(Incase you are wondering what my issue is with this function - If a user closes their browser window before logging off, they will remain 'online')

The question:

Which technique should I use to monitior a users online / offline status?

I could have a cron job running on the server checking each user to see when they were last 'active' on a page, but that would mean an update query on every page land, and a cron job running every 5 mins, 24 hours a day, which is a bigger load on the server than I would like.

The principle is simple, but i can't seem to get an idea of how to resolve this issue in the best fashion.

TIA

Is This A Good Question/Topic? 0
  • +

Replies To: Online / Offline status

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5394
  • View blog
  • Posts: 27,388
  • Joined: 10-May 07

Re: Online / Offline status

Posted 03 November 2008 - 04:23 AM

Using this tutorial I was able to implement the ability to see which users are online by checking the database.
Was This Post Helpful? 1
  • +
  • -

#3 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Online / Offline status

Posted 03 November 2008 - 04:59 AM

Ahh - never thought of approaching the problem by assigning the online / offline database records to it's own table, and then letting the users themselves act as the cron job to update and delete, in correspondance to the $timeout.

These things seem so obvious when put infront of you - i feel a bit stupid now :( lol.

Cheers buddy, you're a champ.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5394
  • View blog
  • Posts: 27,388
  • Joined: 10-May 07

Re: Online / Offline status

Posted 03 November 2008 - 05:20 AM

View Postpemcconnell, on 3 Nov, 2008 - 06:59 AM, said:

Cheers buddy, you're a champ.

I can't take all the credit, it's SkyHawk's tutorial. I've implemented it on a few of my own sites as well, specifically using it for a who's online on one of them. That's how I knew it would be a great fit for your current situation.

Glad it was helpful!
Was This Post Helpful? 0
  • +
  • -

#5 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Online / Offline status

Posted 03 November 2008 - 05:37 AM

Thumbs up to skyhawk aswell :)

The final result is as follows:

<?php
require_once('global.inc.php');
require_once('dbconnect.inc.php');
$timeoutseconds = 300;
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
mysql_query("DELETE FROM tbluseractive WHERE userId = ".$userid." LIMIT 1");
mysql_query("DELETE FROM tbluseractive WHERE userTimeStamp<$timeout");
mysql_query("INSERT INTO tbluseractive (userId, userTimeStamp, userPage) VALUES (".$userid.", $timestamp,'".$_SERVER['PHP_SELF']."')");
$result = mysql_query("SELECT userId FROM tbluseractive WHERE userPage='".$_SERVER['PHP_SELF']."'");
$user = mysql_num_rows($result);
if($user == 1) {
	$usersonthispage = "$user user viewing this page";
} else {
	$usersonthispage = "$user users viewing this page";
}
$result = mysql_query("SELECT userId FROM tbluseractive");
$user = mysql_num_rows($result);
if($user == 1) {
	$usersonline = "$user user online";
} else {
	$usersonline = "$user users online";
}
?>



Which is called into every page via a require()

I modified a few things but as no2pencil stated, full credit to skyhawk and his tutorial.

This post has been edited by pemcconnell: 03 November 2008 - 05:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 jarnail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-June 08

Re: Online / Offline status

Posted 05 November 2008 - 02:34 AM

I think it can also done with cookies. We can set a cookie without expire argument at the time of user login. you can check at any page that this cookie is set or not and can show message of status online/offline. When user will be logout you will destroy this cookie and if user closed browser without pressing logout button then this cookie will also destroy because a cookie is deleted at the close of browser if its expiration time is not mentioned at the time of seeting cookie.

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Online / Offline status

Posted 05 November 2008 - 02:39 AM

Thanks for the reply jarnail, however to accomplish the solution I needed to update the database, which requires a request to the databse.

I have now built the entire system and the online / offline status is working brilliantly using the method above.

Cheers anyway dude
Was This Post Helpful? 0
  • +
  • -

#8 jarnail  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-June 08

Re: Online / Offline status

Posted 05 November 2008 - 03:45 AM

That is great. Keep it up. I just wanna give a new alternative so other can do. I know you have completed your work.
Thanks
Was This Post Helpful? 0
  • +
  • -

#9 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: Online / Offline status

Posted 05 November 2008 - 04:01 AM

Cheers buddy, I 100% agree that it is best to offer multiple options to resolve an issue, and it's much appreciated :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1