关闭浏览器标签时取消会话设置 [英] Unset Session When browser tab is closed

查看:159
本文介绍了关闭浏览器标签时取消会话设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户进入网站时,它会发送默认语言,同时会强制用户从列表中选择一种语言...
当用户选择一种语言时,它会设置$ _SESSION ['language'] = $ _POST ['lg'];
同时,我在此会话中设置了另一个$ _SESSION ['sestime'] = time();
我可以执行以下操作:

Where user enters the website, it sent a default language, at the same time it will force the user to select a language from a list...
When user selects a language it sets a $_SESSION['language'] = $_POST['lg'];
At the same time I set another $_SESSION['sestime'] = time();
with this session I can do this:

if(isset($_SESSION['sestime']) && (time() - $_SESSION['sestime'] > 600)) {
session_unset(); session_destroy();
header("Location: $sred");
exit;
}
$_SESSION['sestime'] = time();

如果10分钟内没有任何活动,它会销毁/删除/取消任何会话,其想法是再次询问lanaguage ...现在,这是有效的",但是我想要的是当用户即将关闭浏览器标签时,检测"网站标签;如果用户关闭标签,则销毁/删除/取消设置"该网站的任何会话...
有可能吗?

With that if there are no activity within 10min it destroy/delete/unset any session, the idea is to ask again for the lanaguage... now, this is "working", but what I'd like is away to "detect" when user is about to close a browser tab, the websites tab... if users close the tab then "destroy/delete/unset" any session for this website...
Is that possible?

推荐答案

会话Cookie通常在没有到期时间的情况下发送,这意味着在关闭浏览器时将其删除,因此会话仍然会丢失.

Session Cookies are usually sent without an expire time which means they are deleted when the browser is closed, so the session is lost anyway.

1)销毁或在用户未单击注销就关闭浏览器时取消设置会话

您可以为会话数据设置过期时间,在每次session_start调用中对其进行测试,并在会话过期时销毁会话:

You can set an expiration time for the session data, test it with each session_start call and destroy the session if it’s expired:

session_start();
if (!isset($_SESSION['EXPIRES']) || $_SESSION['EXPIRES'] < time()+3600) {
    session_destroy();
    $_SESSION = array();
}
$_SESSION['EXPIRES'] = time() + 3600;

2)销毁浏览器标签时的销毁会话

使用自己的方法实现会话超时.使用一个简单的时间戳记来表示最后一个请求的时间,并根据每个请求进行更新:

implement a session timeout with own method. Use a simple time stamp that denotes the time of the last request and update it with every request:

您需要编写与此类似的代码

You need to code something similar to this

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
    // request 30 minates ago
    session_destroy();
    session_unset();
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time

3)如何在PHP中更改会话超时?

session_start(); // ready to go!

$now = time();
if (isset($_SESSION['discard_after']) && $now > $_SESSION['discard_after']) {
    // this session has worn out its welcome; kill it and start a brand new one
    session_unset();
    session_destroy();
    session_start();
}

// either new or old, it should live at most for another hour
$_SESSION['discard_after'] = $now + 3600;

4)当用户离开页面时,将unload事件发送到window元素.这可能意味着很多事情之一.用户可能已经单击了链接以离开页面,或者在地址栏中输入了新的URL.前进和后退按钮将触发事件.关闭浏览器窗口将导致事件被触发.甚至页面重新加载都会首先创建一个卸载事件.

4) The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.

$( window ).unload(function() {
  //use ajax to call another page to session_destroy();
});

问题:如果用户在您的网站上打开了两个或多个标签,该怎么办?如果他们关闭一个选项卡,则另一个选项卡将被有效注销.

Question is: What if the user has two or more tabs open on your site? If they close one tab, the other one would effectively be logged out.

这篇关于关闭浏览器标签时取消会话设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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