drphfy1198 2016-03-31 23:26
浏览 74
已采纳

SESSION循环增加计数器

My session checks if a user is logged , however when I refresh the page the session test runs again and starts to execute the code which then adds +1 to the members online counter everytime . I would like my code to check if the session is true like below but not if I refresh the page or navigate to another page .(Only Once per login) How can this be possible ?

PS MY code is not secure My code is not in any live environment , Security will be applied later to prevent sql injection .

 include('..\db.php');
    $con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
    $guest_timeout = time() -1 * 60;
    $member_timeout = time() -2 * 60;
    $guest_ip = $_SERVER['REMOTE_ADDR'];
    $time = time()

    // This session check adds +1 to my member online counter when page is reloaded
    if(isset($_SESSION['CurrentUser'])){

        //if user is logged
        $sqlt = mysqli_query($con,"DELETE * FROM active_guests WHERE guest_ip='".$guest_ip."'");
        $sqlt2 = mysqli_query($con,"REPLACE INTO active_members VALUES ('".$_SESSION['CurrentUser']."','".$time."')");

    }else{

        //if user not logged
        $sqlt3 = mysqli_query($con,"REPLACE INTO active_guest (guest_ip,time_visited)VALUES ('".$guest_ip."','".$time."')");

    }
    $sqlt4 = mysqli_query($con,"DELETE * FROM  active_guest WHERE time_visited < ".$guest_timeout);
    $sqlt5 = mysqli_query($con,"DELETE * FROM  active_members WHERE time_visited < ".$member_timeout);
    $sqlt6 = mysqli_query($con,"SELECT guest_ip FROM active_guests");
    $online_guests = mysqli_num_rows($sqlt6);
    $sqlt7 = mysqli_query($con,"SELECT username FROM active_members");
    $online_members = mysqli_num_rows($sqlt7);
    ?>

    <div class="container">
    <p>online Guests : <?php echo $online_guests ; ?></p>
    <p>online Members : <?php echo $online_members ; ?></p>
    </div>
  • 写回答

1条回答 默认 最新

  • dtkyayvldeaqhl7151 2016-04-01 00:53
    关注

    You need a unique index on the username column so that REPLACE will replace instead of inserting a duplicate row.

    ALTER TABLE active_members ADD UNIQUE INDEX (username);
    

    You probably also need a unique index on the guest_ip column of active_guests, although it's possible that multiple users could be coming from the same IP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料