2011-11-04 87 views
1

正如我在之前的文章中提到的,我們正在爲特定網站創建聊天。現在這個聊天將不得不檢索在線用戶的名字,並且一旦一個用戶退出聊天,就會自動更新。我們能夠通過使用PHP的孤獨和現在我們正在嘗試使用jQuery來避免經常refreshing.so創建至今這一點,這是我們所擁有的:獲取在線用戶的名稱

 <?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"> 
+8

那麼....有什麼問題? –

+0

您是否想要評論該元刷新標記?如果是這樣,'/ /'不會在html中工作,你需要'' –

+0

問題是,一旦用戶下線,列表不會更新。這意味着離線用戶的名字仍然出現在在線用戶列表中。我們試圖在用戶離線時自動更新在線用戶列表。 –

回答

0

你必須把一個布爾值DB中的用戶(在用戶表中)。當他得到登錄時,即當他進入他的個人資料頁面時,BOOL值應該改爲1,當他得到註銷時,將vlaue更改爲0.