Jump to content

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

Edited by wleorule

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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