如何删除PHP会话? [英] How to delete a PHP session?

查看:43
本文介绍了如何删除PHP会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户退出时,我可能没有正确删除 PHP 会话.我注意到如果我在不关闭浏览器的情况下退出并重新登录,会话 ID 不会改变,但如果我退出,关闭浏览器窗口,打开一个新窗口并登录,会话 ID 会有所不同.我需要做一些不同的事情还是这是正常的行为?我已经使用相同的流程三年了,但最近发生的事情让我觉得也许我需要做一些不同的事情.

It's possible I'm not properly deleting PHP sessions when the user signs out. I've noticed that if I sign out and sign back in without closing the browser, the session ID doesn't change but if I sign out, close the browser window, open a new one and sign in, the session ID will be different. Do I need to be doing something different or is this normal behavior? I've been using the same process for three years but something happened recently that made me think that maybe I need to do something different.

当有人单击退出"时,我基本上会执行以下操作.

Here's what I basically do when someone clicks Sign Out.

<?php

session_start();

if( isSet($_SESSION['FacID']) )
    $facID = $_SESSION['FacID'];    //Want to re-instate this after we destroy the session.

unset($_SESSION);
session_destroy();

if( isSet($_SESSION['FacID']) )
    $_SESSION['FacID'] = $facID;

?>

推荐答案

如果你觉得需要强制一个新的 idhttp://pl.php.net/manual/en/function.session-regenerate-id.php

If you feel the need to force a new id http://pl.php.net/manual/en/function.session-regenerate-id.php

对于您的问题,来自手册:

And to your question, from the manual:

session_destroy() 销毁所有与当前相关的数据会议.它不会取消任何关联的全局变量会话,或取消设置会话 cookie.要再次使用会话变量,必须调用 session_start().

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

为了杀死会话总而言之,就像注销用户一样,会话 ID 也必须取消设置.如果cookie 用于传播会话 ID(默认行为),然后必须删除会话 cookie.setcookie() 可以用于此目的.

In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.

这篇关于如何删除PHP会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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