doujindou4356 2017-02-04 14:37
浏览 37
已采纳

如何计算SESSIONS以显示站点上登录用户的总数

I want display logged in online users on my site like Members Online: 102. I am not catching a perfect and simple way as how to do this. Whether there be a session counting method or any other method to achieve this result. I can do it by using a 0 and 1 column in database table while logging in and logging out but it will not help when the user directly closes the browser and exits the session. That's the real problem. Please help me how to do this. For any instance I am just sharing my login and logout script.

Logout Script

<?php  
    session_start();
    if(isset($_SESSION["zoranelogin"])){
        unset($_SESSION["zoranelogin"]);    
        header("Location: login.php");  
    }
?> 

Login Script

<?php 
session_start();
if(isset($_SESSION['zoranelogin'])){
    header('Location: dashboard.php');
}
include'config/db.php'; 
$msg = null; 
$date = date('Y-m-d H:i:s');

$uname  = (!empty($_POST['uname']))?$_POST['uname']:null;
$pass   = (!empty($_POST['pass']))?$_POST['pass']:null;
$mpass  = (!empty($_POST['pass']))?md5($_POST['pass']):null;

if(isset($_POST['login'])){
    $chklogin = "SELECT * FROM members WHERE mem_uname = :uname AND mem_pass = :pass";
    $chklogiq = $pdo->prepare($chklogin);
    $chklogiq->bindValue(':uname', $uname);
    $chklogiq->bindValue(':pass', $mpass);
    $chklogiq->execute();
    $checklgn = $chklogiq->rowCount();
    $fetch = $chklogiq->fetch();

    if($checklgn > 0){
        session_start();
        $_SESSION['zoranelogin'] = $fetch['mem_id']; 
        header("Location: dashboard.php");      
    }else{
        $msg = "<div class='message-error'>Username and Password are incorrect. Please try again!</div>";
    }
}
?>
  • 写回答

1条回答 默认 最新

  • douyun1950 2017-02-04 15:15
    关注

    It's not possible to detect a closing browser. The best way to know how many users you have on the site is to do it all with sessions. When you use the sessions on file system it's pretty hard because you have to open all the files and read them. So....

    The best way to do it is to save your sessions in the database. There you can do set a user id to an session on login and remove it on logout. Then you can count all sessions with an user is with a simple mysql count script.

    Here a link with a nice tutorial how to do sessions on a database.

    http://www.tonymarston.net/php-mysql/session-handler.html

    You still have one more problem. When a user closes his browser you will not see that. This is because a client don't send a message. i'm gone now. That's why we have a session timeout. you can shorten the timeout to say 2min but then you have a problem that when someone is reading a page for more than 2min he will be kicked. You can compensate that with an ajax script every min to update the session but that will take a lot of server traffic/memory. That's why that is not recommended.

    Advice: Set your sessions to the database with an timeout of 20min. When someone closes his browser you won't see that for max 20min.

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

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: