简体   繁体   English

在php中如何检测用户是否已注销?

[英]How to detect if a user has logged out, in php?

After the user successfully logs in, I store login = true in database. 用户成功登录后,我将login = true存储在数据库中。 But how do I check if the user logged out by closing the browser without clicking the logout button? 但是如何在不单击注销按钮的情况下关闭浏览器来检查用户是否已注销? And also, how do I redirect user who has been inactive for 10 minutes to login page? 此外,如何将已停用10分钟的用户重定向到登录页面?

I am using php and mysql. 我正在使用php和mysql。 Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: Sorry if my question is not clear. 编辑:对不起,如果我的问题不明确。 I did use session to store whether they are logged-in or not. 我确实使用会话来存储它们是否已登录。 But, now I want to store the info in database, so that I can display their status on other pages. 但是,现在我想将信息存储在数据库中,以便我可以在其他页面上显示它们的状态。 Let's say user1 has 3 friends. 假设user1有3个朋友。 When displaying all his friends, user1 want to know whether his friends are online or offline. 当显示他的所有朋友时,user1想知道他的朋友是在线还是离线。 This is what I want. 这就是我要的。 Any advise? 有什么建议吗?

2017 edit: These days, your best bet is using websockets to track presence on a page/site. 2017编辑:这些天,您最好的选择是使用websockets跟踪页面/网站上的状态。


You cannot detect when a user closes their browser or navigates off your site with PHP, and the JavaScript techniques of doing so are so far from guaranteed as to be useless. 您无法检测用户何时关闭浏览器或使用PHP导航您的网站,并且这样做的JavaScript技术远非保证无用。

Instead, your best bet is most likely to store each user's last activity time. 相反,您最好的选择是最有可能存储每个用户的上一个活动时间。

  • Create a column in your user table along the lines of 'last_activity'. 沿着'last_activity'行在用户表中创建一列。
  • Whenever a user loads a page, update their last_activity to the current time. 每当用户加载页面时,将其last_activity更新为当前时间。
  • To get a list of who's online, just query the database for users with last_activity values more recent than 10/20/whatever minutes ago. 要获取在线用户列表,只需查询数据库中last_activity值超过10/20 /几分钟前的用户。

Store the timestamp of each acitivity of the user. 存储用户每个活动的时间戳。 When that time is more than 10 minutes ago, do the logout. 当该时间超过10分钟时,请注销。

In PHP, you could do something like this: 在PHP中,你可以这样做:

session_start();
if (!isset($_SESSION['LAST_ACTIVITY'])) {
    // initiate value
    $_SESSION['LAST_ACTIVITY'] = time();
}
if (time() - $_SESSION['LAST_ACTIVITY'] > 3600) {
    // last activity is more than 10 minutes ago
    session_destroy();
} else {
    // update last activity timestamp
    $_SESSION['LAST_ACTIVITY'] = time();
}

The same can be done on the database instead. 相反,可以在数据库上完成相同的操作。 There you could even get a list of users that are “currently” online. 在那里,您甚至可以获得“当前”在线的用户列表。

You are wondering if you can detect if a user closed his browser. 您想知道是否可以检测用户是否关闭了他的浏览器。 You kinda can with javascript but I would not rely on it (since javascript is easy to disable) But you can not with PHP, since PHP only runs when you are requesting a page. 你有点可以用javascript,但我不会依赖它(因为javascript很容易禁用)但你不能用PHP,因为PHP只在你请求页面时运行。 Not when the page is open. 不是在页面打开时。

To be safe you should track the user's last activity and if it's past a few minutes (5/10) then assume that user is gone. 为了安全起见,您应该跟踪用户的上一次活动,如果过了几分钟(5/10),则假设用户已经离开。 If he does something again though (after 6 minutes for example) then he's back online. 如果他再做一些事情(例如6分钟后),那么他又回到了网上。

If you are trying to track the users that are 'online' then you might consider using a session for the individual user and instead of storing login=true in the db to display their status to you or others, store the last activity time for the user. 如果您尝试跟踪“在线”用户,则可以考虑为单个用户使用会话,而不是在数据库中存储login = true以向您或其他人显示其状态,存储上次活动的时间。用户。 When you pull up your list of online users, create your sql query to only return users with 'last_activity' within the last 10 minutes. 当您提取在线用户列表时,创建您的SQL查询以仅返回在过去10分钟内具有“last_activity”的用户。

You can do this with a combination of ways, 2 at the most i guess, with the one bothering on last activity, you combine it with using jQuery to check for inactivity, that is no mouse or keyboard events for some time, say about 10 to 20 mins, then once the idle time is confirmed, you make an ajax call to a php file that will update your database table showing user offline. 你可以通过多种方式组合实现这一点,最多2个猜测,最后一个活动就是烦恼,你将它与使用jQuery结合起来检查是否存在不活动,一段时间内没有鼠标或键盘事件,比如大约10个到20分钟,然后一旦确认空闲时间,你对一个php文件进行ajax调用,该文件将更新显示用户离线的数据库表。

You can start with: 你可以从:

<script type="text/javascript">
idleTime = 0;
$(document).ready(function () {
//Increment the idle time counter every minute.
var idleInterval = setInterval(timerIncrement, 60000); // 1 minute

//Zero the idle timer on mouse movement.
$(this).mousemove(function (e) {
    idleTime = 0;
});
$(this).keypress(function (e) {
    idleTime = 0;
});
});

function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 19) { // 20 minutes
    $.ajax({
     url: 'update_user.php',
     type: 'POST',
     datatype: 'json',
     data: someData
    });
}
}
</script>   

在会话变量中存储login = true可能更好,以检查用户是否登录。这将解决您的大部分问题;)

Normally you would put such information in sessions. 通常你会把这些信息放在会话中。

$_SESSION['user'] = "A user name";

Then when you want to logout you do: 然后,当您要注销时,您执行以下操作:

session_destroy();

Some more info on sessions here and a tutorial . 会议上一些更多的信息在这里教程

Why dont you use session or cookies for this instead of inserting it in the database. 为什么不使用会话或cookie而不是将其插入数据库。 You can set the cookies by using setcookie() function or either you can make a session vaiable and store the value in it. 您可以使用setcookie()函数设置cookie,也可以将会话变为可变并将值存储在其中。

You would find it better to use the session to monitor the login status. 您会发现使用会话监视登录状态会更好。

Read this information about sessions 阅读有关会话的信息

AFAIK无法检查某个人何时关闭浏览器窗口(或让您的页面转到另一个窗口),因此您需要像上面建议的那样检查活动。

This is an extension to what ' ceejayoz ' said before. 这是' ceejayoz '之前所说的扩展。

Let the users periodically ping the service and tell that they are still logged in. Store last ping time in the session. 让用户定期ping服务并告诉他们仍然登录。在会话中存储最后的ping时间。 If the last ping session is greater than a said time, then ping again to tell that the use is still alive. 如果最后一次ping会话大于所述时间,则再次ping以告知该用途仍然存在。

Store the time when you received the ping in the database. 存储在数据库中收到ping的时间。 If the last ping received is > the keeplive threshold then consider the user has left the site. 如果收到的最后一次ping是> keeplive阈值,则考虑用户已离开该站点。

The following is some untested sample code for you start with. 以下是一些未经测试的示例代码。 You can use the "PING__FREQUENCY" to control how often the frequency in which the user activity will update the last_activity column. 您可以使用“PING__FREQUENCY”来控制用户活动更新last_activity列的频率。

define(PING_FREQUENCY,300); //5 mins
if (($_SESSION['lastPingTime'] + PING_FREQUENCE) > time()) {
    stillLoggedIn(); //execute a function to tell that the user is still logged in
}

function stillLoggedIn() {
    //Do SQL update to mark the last activity time for the user
}

IMHO the best way is to store last activity timestamp in DB on each update of user record. 恕我直言,最好的方法是在每次更新用户记录时在DB中存储最后一个活动时间戳。 After logoff or timeout (maintain timeouts with cronjob) just set it to zero-value and use as flag. 在注销或超时(使用cronjob维护超时)之后,只需将其设置为零值并用作标志。

$user = new User($user_id);
$user->logged_in = (bool)($last_activity > 0);

Sometimes you will need to say smth. 有时你需要说smth。 like "last seen on ...", then leave last activity and just add a boolean flag (tinyint) logged_in to your users table. 比如“上次见过......”,然后留下最后一个活动,只需在你的用户表中添加一个布尔标志(tinyint)logged_in。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM