Flyer5 Posted February 22, 2015 Share Posted February 22, 2015 Hi I have a club site, and on it we have a chatroom, its a basic thing but works ok, but it lacks a whose online list, so you can not see who else is currently on the chat page There is no need to log in to the chat, the users must log into the web site first and the chat name is taken from the session array when they open the chat page, What would be the best way to track the user is on the chat page (and therefore in chat) and output a table of online users? Im guessing its adding the user to a table when they open the page, then checking they are still here after a few minutes? Any advice greatly appreciated! Especially if anyone knows of something already written I can just incorporate! Cheers F5 Quote Link to comment https://forums.phpfreaks.com/topic/294797-advice-re-tracking-users/ Share on other sites More sharing options...
Tom10 Posted February 22, 2015 Share Posted February 22, 2015 Not sure if this would work but $username = $_SESSION['username']; echo $username; Would that work in the chat page? In a table or something Quote Link to comment https://forums.phpfreaks.com/topic/294797-advice-re-tracking-users/#findComment-1506402 Share on other sites More sharing options...
Flyer5 Posted February 22, 2015 Author Share Posted February 22, 2015 Not sure if this would work but $username = $_SESSION['username']; echo $username; Would that work in the chat page? In a table or something Yeah thats what im trying to do, i kinda have this: (but im now lost!) $uname = $_SESSION[_LICENSE_KEY_]['user']['user'] ;// get the persons etano user name $utime = time(); // get the time $uname_check = 'SELECT uname FROM `in_chatroom` WHERE uname = $uname'; $uname_query = mysql_query($uname_check); $num_rows = mysql_num_rows($uname_query); if(!$num_rows) { //if no rows, meaning no uname's in db matched theirs, we will add them. $insert_new = mysql_query("INSERT into in_chatroom (utime, uname) values('$utime','$uname')"); } //end if NOT THERE //this means that they're already in there, so we update info. if($num_rows > 0) { $update = mysql_query('UPDATE `in_chatroom` SET utime=\''.time().'\' WHERE uname = $uname'); if(!$update) die(mysql_error()); } // end UPDATE //if time now - start time > 300 then its been 5 minutes, so we delete $delete_old = mysql_query("DELETE FROM `in_chatroom` WHERE ((".time()."-utime) > 300)"); if(!$delete_old) die(mysql_error()); When you access the page it adds the username and time to the db table ok, but now im stuck! Quote Link to comment https://forums.phpfreaks.com/topic/294797-advice-re-tracking-users/#findComment-1506405 Share on other sites More sharing options...
davidannis Posted February 22, 2015 Share Posted February 22, 2015 (edited) Can't you just "SELECT FROM in_chatroom WHERE 1" and display all the usernames? The table should have everyone who has been active in the last 5 minutes. Edited February 22, 2015 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/294797-advice-re-tracking-users/#findComment-1506416 Share on other sites More sharing options...
wleorule Posted February 22, 2015 Share Posted February 22, 2015 (edited) My advice is to use ajax to submit POST request to script that will keep track of users. e.g: $(document).ready(function(){ var username = <?php echo $username; ?>; function check_online() { $.post('check_if_online.php', { user: username}, function(x){ $(".my_online_table_div").html(x) // In 'x' I return table with online users }); setTimeout(function() { check_online(); }, 3000); // Check again after 3 sec } check_online(); }); And in check_if_online.php you can use a db for storing your online users or a file... Edited February 22, 2015 by wleorule Quote Link to comment https://forums.phpfreaks.com/topic/294797-advice-re-tracking-users/#findComment-1506418 Share on other sites More sharing options...
Flyer5 Posted February 22, 2015 Author Share Posted February 22, 2015 My advice is to use ajax to submit POST request to script that will keep track of users. e.g: $(document).ready(function(){ var username = <?php echo $username; ?>; function check_online() { $.post('check_if_online.php', { user: username}, function(x){ $(".my_online_table_div").html(x) // In 'x' I return table with online users }); setTimeout(function() { check_online(); }, 3000); // Check again after 3 sec } check_online(); }); And in check_if_online.php you can use a db for storing your online users or a file... I know nothing at all about ajax, but this looks grand. Im off to do some reading! Thanks for the advice. Quote Link to comment https://forums.phpfreaks.com/topic/294797-advice-re-tracking-users/#findComment-1506476 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.