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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题