【问题标题】:Destroy PHP Sessions on Browsers Tab Close销毁浏览器选项卡上的 PHP 会话关闭
【发布时间】:2014-03-12 14:49:03
【问题描述】:

我被困住了,我正在处理项目hotbartendersla

我在活动预订中使用了很多会话来处理数据,现在我想在用户关闭浏览器的窗口/选项卡时销毁会话,因为当我打开站点时,选择与我所做的一样。

我用过这个

<script type="text/javascript">
  window.onbeforeunload = function() {

    $.post("mysessionsdestroypage.php",function(data){
    });  

  }
</script>

但是当我跳到第 2 步、第 3 步时,我的会话被破坏并且在第 4 步没有达到数据。 我搜索了很多,但我没有找到可靠的解决方案

【问题讨论】:

  • 当从页面离开时 onbeforeunload 将被调用。所以即使你在向导中进入第二页会话也会被破坏。
  • @Nish 我是如何解决这个问题的?不关闭浏览器会话必须被销毁!
  • 最好的方法是设置会话时间.....

标签: php session session-variables session-cookies session-timeout


【解决方案1】:

首先在要清除会话的页面上设置会话超时

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

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 2014-08-15
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2016-11-08
    • 2013-11-03
    • 2015-12-13
    相关资源
    最近更新 更多