Jump to content

online users


astralaaron

Recommended Posts

I have made a registration / member login on a site in the past.on this new one I am making I would like to learn how I can show how many members / who is online.at first i thought it would be something as simple as a field in the member table `logged` or something and justuse 0 when they are offline and when login update it to 1. but then someone pointed out to me thatwhen they just close the browser they will still be counted online.they mentioned to me something about timestamps, I have never used time stamps and don't know how they work!can anyone shed some light on this for me?thanks!A

Link to comment
Share on other sites

there are 2 methods to do this. Both are prett commonly used, for reasons i'll use the simplier of the two.Here is your use table:

TABLE users(user_id int unique not null auto_increment,username varchar(25) not null,user_pw varchar(32) not null,user_email varchar(200) not null,user_last_logged_in int not null )

The important, and probably strange part of this is the very last colmn.Here is your user login page: assuming that the post data has been sent already

 $user = $_POST['user'];  //You should check if magic_quotes is on, but I don't feel like doing that at the moment. It's a simple 1 line thing. I posted it on here somewhere else. $pw = md5($_POST['pw']); $sql = "SELECT * FROM users WHERE user_name = '$user' and $user_pw='$pw'"; $ans = mysql_query($sql,$yourConnectionVariable) or die(mysql_error()); $userdata = mysql_fetch_assoc($ans); $userdata['user_last_logged_in'] = time();  $updateSql = "UPDATE users SET user_last_logged_in = ".time()." WHERE user_id= ".$userdata['user_id'];  $ansUpdate = mysql_query($updateSql,$yourConnectionVariable) or die(mysql_error());  //Whatever you want to do now, go to a new page etc.

Now on whatever page you're on to check the users online, you would do this:

$sql = "SELECT count(*) as users_online FROM users WHERE (".time()." - user_last_logged_in) < 300";$ans = mysql_query($sql,$yourConnectionVariable) or die(mysql_error());$row = mysql_fetch_assoc($ans);echo "There are ".$row['users_online']." users online right now.";

Link to comment
Share on other sites

there are 2 methods to do this. Both are prett commonly used, for reasons i'll use the simplier of the two.Here is your use table:
TABLE users(user_id int unique not null auto_increment,username varchar(25) not null,user_pw varchar(32) not null,user_email varchar(200) not null,user_last_logged_in int not null )

The important, and probably strange part of this is the very last colmn.Here is your user login page: assuming that the post data has been sent already

 $user = $_POST['user'];  //You should check if magic_quotes is on, but I don't feel like doing that at the moment. It's a simple 1 line thing. I posted it on here somewhere else. $pw = md5($_POST['pw']); $sql = "SELECT * FROM users WHERE user_name = '$user' and $user_pw='$pw'"; $ans = mysql_query($sql,$yourConnectionVariable) or die(mysql_error()); $userdata = mysql_fetch_assoc($ans); $userdata['user_last_logged_in'] = time();  $updateSql = "UPDATE users SET user_last_logged_in = ".time()." WHERE user_id= ".$userdata['user_id'];  $ansUpdate = mysql_query($updateSql,$yourConnectionVariable) or die(mysql_error());  //Whatever you want to do now, go to a new page etc.

Now on whatever page you're on to check the users online, you would do this:

$sql = "SELECT count(*) as users_online FROM users WHERE (".time()." - user_last_logged_in) < 300";$ans = mysql_query($sql,$yourConnectionVariable) or die(mysql_error());$row = mysql_fetch_assoc($ans);echo "There are ".$row['users_online']." users online right now.";

thank you very much, ill mess with it in a bit :)
Link to comment
Share on other sites

There's one thing I can't get:That works for registered users. How can I count guests?Let's guess that we store the SID (if we're using sessions), but how can I delete that register when the user close the window? Can it be done?

Link to comment
Share on other sites

It's not so much that you do it after the guest closes the window, it's more before the code to say "x people are online" you must first call a DELETE FROM table WHERE

- insertedTimeColumn >(some number of seconds, minutes or hours in timestamp form).
Link to comment
Share on other sites

You could use AJAX with the JS window.onunload event (don't forget to set the third parameter of the xmlHttpRequest.open() method to false so that the command goes through before the window closes).

Link to comment
Share on other sites

But then the new page can just re-register the user in the database. Inefficient maybe though...

Link to comment
Share on other sites

You just can't reliably detect when someone leaves your site. Using Javascript is only a hack, if someone has JS disabled then you'll always show them as being online.One thing you can do is to track this using the session. The easiest way would be to use a custom session handler and keep all your session information in the database, then it's easy to count how many active sessions there are. That will give you the total number of sessions though, not just registered users, but if you keep a variable in the session to say whether or not they are registered then you can get the total number of sessions open and subtract from that the number of those that are registered. That would give you the number of registered users and the number of anonymous users. Check this topic for more info:http://w3schools.invisionzone.com/index.php?showtopic=9731

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...