如何计算SESSIONS以显示网站上登录的用户总数 [英] How to count SESSIONS to display total number of logged in users on the site

查看:173
本文介绍了如何计算SESSIONS以显示网站上登录的用户总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望显示在我的网站上登录的在线用户,如会员在线:102 。我没有抓住一个完美而简单的方法,因为如何做到这一点。是否有会话计数方法或任何其他方法来实现此结果。我可以通过在登录和注销时在数据库表中使用0和1列来实现,但当用户直接关闭浏览器并退出会话时,这无济于事。这是真正的问题。请帮助我如何做到这一点。对于任何实例,我只是分享我的登录和注销脚本。

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.

注销脚本

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

登录脚本

<?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>";
    }
}
?>


推荐答案

无法检测关闭的浏览器。了解您在网站上拥有多少用户的最佳方式是使用会话完成所有操作。当您在文件系统上使用会话时,这非常困难,因为您必须打开所有文件并阅读它们。所以......

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....

最好的方法是将会话保存在数据库中。您可以在登录时将用户标识设置为会话,并在注销时将其删除。然后你可以计算所有会话与用户是一个简单的MySQL计数脚本。

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

你还有一个问题。当用户关闭他的浏览器时,你不会看到它。这是因为客户端不发送消息。我现在走了。这就是为什么我们有会话超时。你可以缩短超时时间来说2分钟,但你有一个问题,当有人正在阅读一个页面超过2分钟时,他将被踢。你可以用ajax脚本每分钟补偿一次以更新会话,但这会占用大量的服务器流量/内存。这就是为什么不推荐。

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.

建议:将会话设置为数据库,超时时间为20分钟。当有人关闭他的浏览器时,你不会在最长20分钟内看到它。

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.

这篇关于如何计算SESSIONS以显示网站上登录的用户总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆