浏览器关闭时的php autologout

时间:2014-07-22 05:17:50

标签: php session

session_set_cookie_params(0);无效。如果浏览器关闭,网站应自动注销。请帮帮我。

<?php
    session_set_cookie_params(0);
    session_start();
    include('connection.php');

    unset($_SESSION['email']);
    session_destroy();

    header('location:index.php');
?>

1 个答案:

答案 0 :(得分:2)

您可以使用javascript执行此操作,您所要做的就是删除名为&#34的cookie; PHPSESSID&#34;这允许服务器知道客户端的会话ID是什么,

function removeCookie(cookieName)
{
    cookieValue = "";
    cookieLifetime = -1;
    var date = new Date();
    date.setTime(date.getTime()+(cookieLifetime*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    document.cookie = cookieName+"="+JSON.stringify(cookieValue)+expires+"; path=/";
}

这是删除cookie的功能,您只需在用户关闭浏览器时调用该函数

window.onbeforeunload = closingCode;
function closingCode()
{
  removeCookie("PHPSESSID");
}