我怎么能显示在线或离线用户从它的PHP,MySQL和阿贾克斯会议在线、离线、它的、会议

2023-09-10 18:27:06 作者:超负荷

请告诉我,这样我怎么能显示用户是在线还是离线通过访问其会话。 这是PHP和MySQL查询

标题(内容类型:应用程序/ JSON); $阵列=阵列(); $水库=请求mysql_query(SELECT * FROM`posts` WHERE状态= 1); 如果(mysql_num_rows($水库)大于0){     而($行= mysql_fetch_assoc($水库)){         $数组[] = $行['USER_ID']; //这增加了每个在线用户ID阵列     } } 回声json_en code($阵列);

我尝试这个$ C C这个$是JS文件

<脚本类型=文/ JavaScript的>    $(文件)。就绪(函数(){      的setInterval(函数(){       $阿贾克斯({            网址:status.php,            数据类型:JSON,            键入:GET,            成功:功能(数据){                如果(data.length大于0){//如果至少1是在线                   $('状态')。每个(函数(){//遍历每个用户的职位                       VAR用户ID = parseInt函数($(本).attr('身份证')取代('用户','')); //得到公正的用户ID#                       如果($。inArray(用户ID数据)!== -1){//如果用户ID#返回的数据数组中设置为在线                            $(本)的.css({背景:绿色});                       }其他{//否则,如果用户ID#不返回的数据数组设置为脱机                            $(本)的.css({背景:'灰色'});                       }                   });                }                其他{//如果没有人在线,将所有离线                    $('状态')的CSS({背景:'灰色'});                }            }         });     },2000); //只是用于测试2秒。设置为15秒时,code完全有效。    });  < / SCRIPT> php mysql 怎么导出到excel

但据我每次保存我的状态数据库,然后在此表现得保存在数据库是它给我造成这code未按照我的requerements运行。 我想要什么

当用户关闭其浏览器上,逻辑上它会是毁灭的话,我想它的状态显示脱机

2,如果用户保持他会那么它的状态为在线

解决方案

没有直着AFAIK做到这一点。有没有正确的方法来检测用户是否已经关闭了浏览器。一种方法我已经看到了常用的解决[您的环境中]这一特定问题是由的帖子使用 last_activity_timestamp 字段表。每次用户做任何活动时, last_activity_timestamp 与新的时间进行更新。

  $水库=请求mysql_query(更新`posts` SET last_update_timestamp = NOW()WHERE user_ID的= {$ currentUserId};);
 

然后在你的code检查用户是否在线,你也可以检查,因为用户的,像这样的最后一项活动在一定时间段(比如20分钟)是否已经过去:

  $水库=请求mysql_query(SELECT * FROM`posts` WHERE状态= 1 AND TIMESTAMPDIFF(MINUTE,last_activity_timestamp,NOW())→20;);
 

如果用户的最后一个活动是20多分钟的时候,那么他有效地退出。

please tell me way how can i show user is online or offline by accessing its session. this is php and mysql query

header('Content-Type: application/json');
$array = array();

$res = mysql_query("SELECT * FROM `posts` WHERE status=1");
if(mysql_num_rows($res) > 0){
    while($row = mysql_fetch_assoc($res)){  
        $array[] = $row['user_id'];  // this adds each online user id to the array         
    }
}
echo json_encode($array);

i try this code this is js file

<script type="text/javascript">    
   $(document).ready(function() {                               
     setInterval(function(){
      $.ajax({
           url: 'status.php',
           dataType: "json",
           type: 'GET',
           success: function(data) {
               if (data.length > 0){  // if at least 1 is online
                  $('.status').each(function(){  // loop through each of the user posts
                      var userid = parseInt($(this).attr('id').replace('user','')); // get just the userid #
                      if($.inArray(userid, data) !== -1){  // if userid # in the returned data array set to online
                           $(this).css({background: 'green'});  
                      } else{  // else if userid # not in the returned data array set to offline
                           $(this).css({background: 'grey'});  
                      }
                  });
               } 
               else { // if no one is online, set all to offline
                   $('.status').css({background: 'grey'});
               }

           }
        });
    }, 2000); //2s just for testing. Set to 15s when code fully works.
   });
 </script>

but this code is not run according to my requerements it according to what i save in DB every time i save my status in DB then on the behave of this it give me result. what i want

when user close its browser then logically its session is destroy then i want to show its status offline

2.if user maintain his session then its status is online

解决方案

There is no straight forward to do this afaik. There is no proper way to detect whether a user has closed his browser. One method I have seen commonly used to solve this specific problem [within your context] is by using a last_activity_timestamp field in the posts table. Each time a user does any activity, the last_activity_timestamp is updated with the new time.

$res = mysql_query("UPDATE `posts` SET last_update_timestamp = NOW() WHERE user_id = {$currentUserId};");

Then in your code for checking whether a user is currently online, you can also check whether a certain time period (say 20 minutes) has elapsed since the last activity of a user like so:

$res = mysql_query("SELECT * FROM `posts` WHERE status=1 AND TIMESTAMPDIFF(MINUTE, last_activity_timestamp, NOW()) > 20;");

If the last activity of a user was more than 20 minutes old, then he is effectively logged out.