【问题标题】:get names of online users获取在线用户的姓名
【发布时间】:2011-12-21 19:43:09
【问题描述】:

正如我在之前的帖子中提到的,我们正在为特定网站创建聊天。现在,此聊天必须检索在线用户的姓名,并且一旦一个用户退出聊天,它就会自动更新。我们能够单独使用 PHP 来创建它,现在我们正在尝试使用 jquery 来避免经常刷新。到目前为止,这就是我们所拥有的:

     <?php
     session_start();       //Configuation
     ?>

     <link rel="stylesheet" type="text/css" href="http://www.pinoyarea.com/videochat/css/winterblues.css">

    <?php

       $name = $_SESSION['username'];
       $room = $_SESSION['room'];
       $user = $_SESSION['user'];
       if($name == NULL || $room == NULL || $user = NULL)
       {
          echo "<script>window.location = 'http://www.pinoyarea.com/index.php?p=member/signup';</script>";
       }
     include "connect.php";

     $timeoutseconds = 60; // length of session, 20 minutes is the standard
     $timeoutseconds_idle = 30;
     $timestamp=time();
     $timeout=$timestamp-$timeoutseconds;
     $timeout_idle=$timestamp-$timeoutseconds_idle;
     $PHP_SELF = $_SERVER['PHP_SELF'];
     if (!empty($_SERVER["HTTP_CLIENT_IP"]))
     {
       //check for ip from share internet
       $ip = $_SERVER["HTTP_CLIENT_IP"];
     }
     elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
     {
        // Check for the Proxy User
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
     }
     else
     {
       $ip = $_SERVER["REMOTE_ADDR"];
     }
     $temp = @mysql_query("SELECT * FROM useronline WHERE online_name='$name' AND online_user='$user' AND online_room='$room'");
      $rowie = @mysql_num_rows($temp);
     // Add this user to database
      $loopcap = 0;
      while ($loopcap<3){
      if($rowie == 0 AND $name != NULL)
      {
      @mysql_query("insert into useronline values('$timestamp','$ip','$PHP_SELF','$name','$room','$user')");
      }
      else
         {
         } // in case of collision
      $timestamp = $timestamp+$ip{0}; $loopcap++;
      }

       // Delete users that have been online for more then "$timeoutseconds" seconds
          mysql_query("delete from useronline where timestamp<$timeout");

       //Modified

       // Select users online
       $result = @mysql_query("select distinct online_name from useronline"); 
       $result2 = @mysql_query("SELECT distinct online_name FROM useronline WHERE online_room='$room'");
       $user_count = @mysql_num_rows($result2);
        mysql_free_result($result);
       @mysql_close();


     // Show all users online
     echo '<table name="tableonline" width="180px">';
     if ($user_count==1) 
      {
   echo '<tr><th>';
   echo '<font size="1px" style="font-family:arial;"><strong>'.$user_count.' Toozer Online</th></tr>';
      } 
      else 
      {
     echo '<tr><th>'.$user_count.' Toozers Online</strong></font></th></tr></table>';
      }

     echo "</table><br /><table width='180px'>";
     while($cell = mysql_fetch_array($result2))
    {
   $timestamping = $cell["timestamp"];
   if($timestamping >= $timeout_idle && $timestamping < $timeout)
  {
      $src = "http://www.pinoyarea.com/images/videochat/user-offline.png";
  }
   else
  {
    $src = "http://www.pinoyarea.com/images/videochat/user-online.png";
  }
      echo '<tr><td><img src="'.$src.'"/><font size="1px" style="text-decoration:none;font-family:tahoma;"></td><td>'.$cell["online_name"].'</font>';
      echo '<br /></td></tr>';
     }
    echo '<table>';

   ?>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">  </script>
  <script>
  $(document).ready(function() {
  $("#tableonline").load("online_users.php");
   var refreshId = setInterval(function() {
   $("#tableonline").load('online_users.php?randval='+ Math.random());}, 3000);
   });
   </script>

    //<META HTTP-EQUIV="Refresh" CONTENT="10">

【问题讨论】:

  • 那么....有什么问题?
  • 您是否要注释掉该元刷新标签?如果是这样,// 在 html 中不起作用,你需要 &lt;!-- ... --&gt;
  • 问题是,一旦用户下线,列表就不会更新。这意味着离线用户的名称仍然出现在在线用户列表中。我们尝试在用户离线时自动更新在线用户列表。

标签: php ajax refresh


【解决方案1】:

您必须在数据库中(在用户表中)为用户输入一个布尔值。当他登录时,即当他进入他的个人资料页面时,BOOL 值应更改为 1,当他注销时,将 vlaue 更改为 0。

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2013-03-18
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多