本文介绍了销毁浏览器选项卡上的 PHP 会话关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被困住了,我正在做项目 hotbartendersla
i am stucked, i am working on Projects hotbartendersla
我在事件预订中使用了很多会话来处理数据,现在我想在用户关闭浏览器的窗口/选项卡时销毁会话,因为当我打开站点时,选择仍然和我一样.
i have used a lot of sessions to process data in event booking, now i want to destroy the session when user closed the window/tab of browser,because when ever i open site the selection remains same as i did.
我用过这个
<script type="text/javascript">
window.onbeforeunload = function() {
$.post("mysessionsdestroypage.php",function(data){
});
}
</script>
但是当我跳到第 2 步、第 3 步时,我的会话被破坏了,第 4 步没有到达数据.我搜索了很多,但我没有找到可靠的解决方案
but when i jumped to on step 2,step 3, my sessions are destroyed and data don't reached on step 4.i searched alot but i found no reliable solution for this
推荐答案
首先在要清除Session的页面设置Session超时
First of all Set Session time out on the page where you want to clear Sessions
<?php
// destroy session in 15 minutes, 900 ms =15 minutes
if (isset($_SESSION['LAST_ACTIVITY_step1']) && (time() - $_SESSION['LAST_ACTIVITY_step1'] > 900)) {
header("Location:http://www.hotbartendersla.com/session-destroy");
}
$_SESSION['LAST_ACTIVITY_step1'] = time(); // the start of the session.
?>
make a new page(in wordpress) to destroy sessions and add template to that page,
pagedestroy-sessions.php
<?php
/*
Template Name: Destroy Sessions
*/
session_start();
include('header.php');
session_destroy();
session_unset();
?>
<h2>Session Has Been Destroyed </h2>
<?php
include('footer.php');
?>
<script type="text/javascript">
function redirect() {
document.location = 'https://www.hotbartendersla.com/event-booking-step-1';
}
<!- after 1 second redirect to event-booking-step-1 Page-->
setTimeout(redirect(),1000);
</script>
这篇关于销毁浏览器选项卡上的 PHP 会话关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!