Jump to content

counting user online help????????PLEASE


rajmohan

Recommended Posts

alright, well you could easily just add a couple of extra fields into your users table, which i am assuming you have setup like this:

id | username | password | email                | etc...
----------------------------------------------------
1    user1        pass1        email@email.com  etc..

alright, well i will show you how to do a timeout as well, so after a period of time the session is destroyed and they are signed out. can be useful. otherwise...no. :) first at 2 more fields called 'status' and 'timeout'. make timeout a varchar with 255 values and status a smallint with the value of 1 and a default of 0.
we will use this rule: 0 = offline and 1 = online.

ok...
now...

when the user signs in add this to your code, note you can take out the timeout bit if you want.

[code=php:0]
$timeout = time() + (30 * 60); //you can change the 30 to watever, the 30 stands for 30 minutes, but to get it we must multiply by 60.

mysql_query("UPDATE `table` SET `status`='1', `timeout`='".$timeout."' WHERE `username`='".$username."'"); //remember to change the table and the username variable if you need to.
[/code]

now, when they logout do exactly the same thing except set status to 0 and timeout to nothing.

if you are doing the timeout then you can read this otherwise skip it. if you have a log file or a file that is included in every page add this to it, remember to read it and change any values.

[code=php:0]
mysql_query("UPDATE `table` SET `status`='0', `timeout`='0' WHERE `timeout`<='".time()."'"); //this will remove any users who's timeout has passed...

if(isset($_SESSION['user'])){ //checks to see if the user is logged in
$user_timeout = mysql_result(mysql_query("SELECT `timeout` FROM `table` WHERE `username`='".$_SESSION['user']."'"),0); //just grabs the users timeout, remember to change any values.
if($user_timeout <= time()){ //checks if the users timeout has passed, because we added the 30 minutes to it, if time() is greater then the user timeout we will sign them out.
mysql_query("UPDATE `table` SET `status`='0', `timeout`='0' WHERE `username`<='".$_SESSION['user']."'"); //this will remove the user
header("Location: login.php"); //redirect the user to the login page again. or you can redirect them to an error page explaining why they have been signed out.
}
}
[/code]

now for the users online... simply do this:

[code=php:0]
$users_online = mysql_num_row(mysql_query("SELECT * FROM `table` WHERE `status`='1'"));
echo "There are currently ".$users_online." users online!";
[/code]

you can spice it up a bit by adding a timeout in if they are inactive for to long. so whenever they refresh the page change the timeout to another 30 minutes or whatever.

good luck.
Link to comment
Share on other sites

Somehow you'll also need to check for any users with timeouts that have already passed, have gone away, but are still recorded as being logged in.

For example, if someone clicks the CLOSE button on the browser WITHOUT logging out, the database will still think they are logged in.

There would be ways around it - but they get sort of complicated.
Link to comment
Share on other sites

I would add a 'last_page' timer..
that, whenever they go to a page.
it updates that (simply enter the timestamp into the 'last_page' column..)

then, for the list..
do a query for all the usernames where the 'last_page' < timestamp-60.

change the 60 to whatever amount of seconds you want...
Link to comment
Share on other sites

if you read the code i did place this:
[code=php:0]
mysql_query("UPDATE `table` SET `status`='0', `timeout`='0' WHERE `timeout`<='".time()."'"); //this will remove any users who's timeout has passed...
[/code]
which updates the table for any users that closed the brower and there time has run out...
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.