开发者

refresh problem in online users list

开发者 https://www.devze.com 2023-03-22 10:20 出处:网络
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 cli

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,statu开发者_如何学编程s)   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


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')");
}


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消