Log in

View Full Version : Session Variables



InNeedofHelp
06-24-2006, 07:55 PM
Hey DD,
Quick question: is there any way to count active sessions? What i mean by that is, like you see on forums, Users online: 250. How does it get that 250? I'm assuming it counts the current sessions or something similar. Is that how one would go about that? Or should i put a table in my database that updates each time someone logs in or logs out?

Thanks.

Twey
06-24-2006, 07:57 PM
It increments it when the user logs on, then stores the time the user was last seen. Each time a page is accessed, it goes through the database and marks each user as logged out if they haven't been seen for the last, say, five minutes, and decrements the counter.

InNeedofHelp
06-24-2006, 08:00 PM
Ah, OK.

How would one go about scripting that?

Thanks.

djr33
06-24-2006, 08:18 PM
Or should i put a table in my database that updates each time someone logs in or logs out?Yep :)


As for scripting... the logic isn't that hard... you've got the right idea. Now just get an idea of how php works, along with mysql, and there ya go. Here's a good tutorial site: http://php-mysql-tutorial.com/

Twey
06-24-2006, 08:53 PM
<?php
mysql_connect("localhost", "username", "password");
mysql_select_db("database");
mysql_query("update userstable set lastseen=NOW() where id=$userid;");
mysql_query("update userstable set logged=0 where logged=1 and DATE_SUB(NOW(), IN
TERVAL 5 MINUTE) > lastseen;");
$rs = mysql_query("select * from userstable where logged=1;");
$numusers = mysql_num_rows($rs);
?>Predefined variables: $userid -- ID of user in database
Structure of userstable:
+----------+------------+
| Field | Type |
+----------+------------+
| id | int(11) |
| lastseen | datetime |
| logged | tinyint(1) |
+----------+------------+

InNeedofHelp
06-24-2006, 09:32 PM
Holy cow. That's way above my level of PHP. :p

I'll work with it, see what I can do.

Thanks, Twey.

Twey
06-24-2006, 11:25 PM
It's more SQL than PHP.