Jump to content

Advice re tracking users


Flyer5

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/294797-advice-re-tracking-users/
Share on other sites

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! 

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...

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.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.