简体   繁体   English

PHP 在页面刷新或退出时销毁会话

[英]PHP Destroy Session on Page Refresh or Exit

I am working on a PHP application that allows users to process a payment online.我正在开发一个允许用户在线处理付款的 PHP 应用程序。 The process is as follows;过程如下;

  • user visits /index.php and selects button to 'pay now'用户访问/index.php并选择“立即付款”按钮
  • user is redirected to worldpay and payment is processed用户被重定向到 worldpay 并处理付款
  • after successful payment worldpay sends a success parameter to my /callback.php and this is where I set $_SESSION[status] = 'success' before redirecting back to /index.php成功后支付WorldPay的发送success参数来我/callback.php ,这是我设置$_SESSION[status] = 'success'重定向回到之前/index.php

This all works as expected, if the payment was a success I am able to display a success message, however the message remains even when the user visits other pages of the site and returns to /index.php , it also remains if browser tab is closed and re-opened.这一切都按预期工作,如果付款成功,我可以显示成功消息,但是即使用户访问站点的其他页面并返回到/index.php消息仍然存在,如果浏览器选项卡是关闭并重新打开。

In my /callback.php file I have;在我的/callback.php文件中;

session_start();
// get status sent by worldpay url
$status= $_GET['status'];

if($status=== 'success') {
    $_SESSION['status'] = 'success';
    header('Location: index.php');
    exit();
}

In my /index.php I am checking for the session and displayin the message as below;在我的/index.php我正在检查会话并显示如下消息;

if ( isset($_SESSION['status']) && ($_SESSION['status'] === 'success') ) {
    echo 'success msg';
}

I only need to display the success message once to the user.我只需要向用户显示一次成功消息。

How can I destroy $_SESSION['status'] whenever the user;我怎样才能销毁$_SESSION['status']每当用户;

  • refreshes the page刷新页面
  • leaves the page离开页面
  • closes tab关闭标签

Should I be using cookies or some other method?我应该使用 cookie 还是其他方法?

Maybe you can unset the $_SESSION['status'] after displaying the success message ONCE.也许您可以在显示成功消息一次后取消设置 $_SESSION['status'] 。

if ( isset($_SESSION['status']) && ($_SESSION['status'] === 'success') ) {
   echo 'success msg';
   unset($_SESSION['status']);
}

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

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