简体   繁体   English

刷新在线用户列表中的问题

[英]refresh problem in online users list

I have a problem with the online users list. 在线用户列表有问题。 The code works fine, all the online users are displayed on the screen but when I click on refresh, the same user's email is displayed again and when I click on refresh for the second time the user's email is displayed three times and so on. 该代码工作正常,所有在线用户都显示在屏幕上,但是当我单击刷新时,将再次显示同一用户的电子邮件,而当我第二次单击刷新时,该用户的电子邮件将显示三遍,依此类推。

Here is my code: 这是我的代码:

<?php

require_once("db.php");

db_connect();

session_start();


$player_timeout = time() - 5 * 60;

$time = time();

if (isset($_SESSION['email'])) {

$login=mysql_query("insert into activePlayer(player_email,time_visited,status)   values('".$_SESSION['email']."','".$time."', 'true')");

} }

else

{echo "You are not logged in";}

$tmout = mysql_query("DELETE FROM activePlayer WHERE time_visited < ".$player_timeout);

$online_member = mysql_query("SELECT player_email FROM activePlayer");

$row=mysql_num_rows($online_member);

$member_row=mysql_fetch_array($online_member);

echo "Welcome &nbsp; '".$_SESSION['email']."'";

?>

<body>

<select > <?php  

 if ($row<1)
      {
        echo "&nbsp;";
      }
      else
      {?> <p><p>Online Players:<option><?php echo $member_row['player_email'];?>  
     </option>}
      <?php for ($i=1;$i<$row;$i++)

{
      $member_row=mysql_fetch_array($online_member);?>
      <p><p>Online Players:<option><?php echo $member_row['player_email']; }}?>   
 </option></select>
</body>

please how can I solve this problem 请问我该如何解决这个问题

What you need to do is a redirect: 您需要做的是重定向:

if (isset($_SESSION['email'])) {
  mysql_query("INSERT INTO activePlayer (player_email,time_visited,status)
               VALUES ('".$_SESSION['email']."','".$time."', 'true')");

  unset($_SESSION['email']);
  header("Location: otherpage.php"); // or it can be the same page
}

Every time you refresh you insert a row into the db if the user is logged in. You have to check if the user already exists in the db and update his record instead. 每次刷新时,如果用户已登录,则在数据库中插入一行。您必须检查用户是否已存在于数据库中,而是更新其记录。 If he has no record then just create a new as you do. 如果他没有记录,那么只需创建一个新记录即可。

$hasRow = mysql_query("SELECT * FROM activePlayer WHERE player_email='".$_SESSION['email']."' LIMIT 1");
if(mysql_num_rows($hasRow) > 0) {
    $login = mysql_query("UPDATE activePlayer SET visited=".time()." WHERE player_email='".$_SESSION['email']."'");
} else {
    $login=mysql_query("insert into activePlayer(player_email,time_visited,status)   values('".$_SESSION['email']."','".$time."', 'true')");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM