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! Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/ 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. Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309234 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.... Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309238 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? Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309243 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. Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309247 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? Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309250 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. Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309618 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 Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309623 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"; ?> Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309705 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(); } ?> Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309738 Share on other sites More sharing options...
Trium918 Posted July 28, 2007 Author Share Posted July 28, 2007 *bump* Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309821 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. Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309833 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.'; Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309844 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']." "; } ?> Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309872 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; Link to comment https://forums.phpfreaks.com/topic/62113-solved-active-users-online/#findComment-309884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.