Trium918 Posted July 27, 2007 Share Posted July 27, 2007 I am trying to keep track of the active users who are online. How would I implement a system of this nature? Thanks in Advance! Quote Link to comment Share on other sites More sharing options...
Caesar Posted July 27, 2007 Share Posted July 27, 2007 Session and a sessions table in your MySQL database would be one approach. Quote Link to comment Share on other sites More sharing options...
dj-kenpo Posted July 27, 2007 Share Posted July 27, 2007 best way is to not use the default session path. make your own, then count how many session files there are in that directory. google will reveal the exact code of it. it's better than tryin to do any elaborate sql work to 'try' to figure out who's still logged in.... Quote Link to comment Share on other sites More sharing options...
Trium918 Posted July 27, 2007 Author Share Posted July 27, 2007 Basically, I just need to count the session that are activated when the users logs in? Quote Link to comment Share on other sites More sharing options...
dj-kenpo Posted July 27, 2007 Share Posted July 27, 2007 ya, but to do that correctly you need to change the path. otherwise if you're on a shared server you're counting other stuff. if you even can with the defualt setup. Quote Link to comment Share on other sites More sharing options...
Trium918 Posted July 27, 2007 Author Share Posted July 27, 2007 ya, but to do that correctly you need to change the path. otherwise if you're on a shared server you're counting other stuff. if you even can with the defualt setup. What path are you referring to? Quote Link to comment Share on other sites More sharing options...
Trium918 Posted July 28, 2007 Author Share Posted July 28, 2007 I was thinking more of a switch that counts each individual user when they log in. Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 28, 2007 Share Posted July 28, 2007 http://www.phpfreaks.com/forums/index.php/topic,151986.0.html Quote Link to comment Share on other sites More sharing options...
Trium918 Posted July 28, 2007 Author Share Posted July 28, 2007 http://www.phpfreaks.com/forums/index.php/topic,151986.0.html Have a piece of code on each page that updates a field in the users table with the current datetime, then in your "Currently Online" box, select all users from the table that have accessed a page within the last n minutes. I already have a field in the members table called last_visit. I was thinking maybe I can just update last_visit, so my query would look something like the one below, correct? <?php $timeoutSeconds = 600; $timeout = time() - $timeoutSeconds; $query ="SELECT * FROM user_table WHERE last_visit < $timeout"; ?> Quote Link to comment Share on other sites More sharing options...
Trium918 Posted July 28, 2007 Author Share Posted July 28, 2007 Here is the query that I implemented and it is not working. There are no errors, but I am getting an echo "No results"; I am trying to determine all active users online. <?php $timeoutSeconds = 600; $num_online = 0; $currentTime = time(); $timeout = $currentTime - timeoutSeconds; $online_query = "SELECT * FROM members_info WHERE last_visit < $timeout"; if($online_result=mysql_query($online_query)){ if (mysql_num_rows($online_result)) { $num_online = mysql_num_rows($online_result); if($num_online == 1) { echo "$num_online"; }else { echo "$num_online"; } }else { echo "No results found"; } } else { echo "Query failed<br />$online_query<br />" . mysql_error(); } ?> Quote Link to comment Share on other sites More sharing options...
Trium918 Posted July 28, 2007 Author Share Posted July 28, 2007 *bump* Quote Link to comment Share on other sites More sharing options...
Hughesy1986 Posted July 29, 2007 Share Posted July 29, 2007 It should be making a error as $timeout = $currentTime - timeoutSeconds; Should be $timeout = $currentTime - $timeoutSeconds; Other then that the code looks ok, I have made a whos online script before and what people where saying above about sessions etc is not needed, and the methods you are using are fine. Quote Link to comment Share on other sites More sharing options...
corbin Posted July 29, 2007 Share Posted July 29, 2007 I would just update a column everytime a page loads or something like that with a timestamp.... Then simply do something like: $ctime = time() - 600; //10 minutes in the past $q = mysql_query("SELECT COUNT(user_id_or_some_other_column) FROM users_or_what_ever_its_named WHERE last_active >= {$ctime}"); $r = mysql_fetch_row($q); echo 'There are ' . $r[0] . ' users online.'; Quote Link to comment Share on other sites More sharing options...
Trium918 Posted July 29, 2007 Author Share Posted July 29, 2007 I created to users accounts. User1 and User2 If I log in as User1 everthing is fine, but when I log User2 in then User1 online status is deleted from the members_online table. There should be a count of two users online. What is causing the problem of one User account being deleted? <?php // connect to database // retrieve # of users online // I am having problems with this section $timeoutSeconds = 60; $timeout = time() - timeoutSeconds; $query = "DELETE FROM members_online WHERE timestamp < $timeout"; mysql_query($query); /////////////////////////////////////////////////////////// $query = "SELECT username FROM members_online"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo $row['username']." "; } ?> Quote Link to comment Share on other sites More sharing options...
corbin Posted July 29, 2007 Share Posted July 29, 2007 $timeout = time() - timeoutSeconds; Should be $timeout = time() - $timeoutSeconds; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.