如何在 PHP 中使用户会话过期?
有些人说使用 unset($_SESSION["..."])
,有些人说 session_unset()
,有些人说 $_SESSION = array()
code> ,有些人说 session_destroy()
,我是说“看在上帝的份上,这东西变得令人困惑,有人可以解释一下哪种是注销用户的正确/安全方法”以及什么吗?是用来做什么的?
赞赏...
Some people say use unset($_SESSION["..."])
and some say session_unset()
and some say $_SESSION = array()
and some say session_destroy()
and I am saying "for God's sake, this stuff is getting confusing, can someone please explain me which is the correct/secure way to log the user out" and what is used for what?
Appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
RTM
RTM
这是实体之间的区别,
您可以删除会话中的单个变量
,这将删除会话中的所有变量,但不会删除会话本身,
这会破坏会话变量
Here is the difference between the entities
you can remove a single variable in the session
this would remove all the variables in the session, but not the session itself
this would destroy the session variables
首先,
session_destroy()
与其他方法不一样。这将破坏服务器上的当前会话数据,但不会取消设置任何变量。它只是session_start()
的对应部分。session_unset()
是已弃用相当于执行$_SESSION = array()
。后者和unset($_SESSION["..."])
的不同之处仅在于unset()
路由只会取消设置单个会话变量,即以[...]
命名的一个。切勿执行unset($_SESSION)
,因为这会干扰会话机制本身。旧问题参考。
First of all,
session_destroy()
is not the same as the other methods. This one will destroy the current session data on the server, but will not unset any of the variables. It's simply the counterpart tosession_start()
.session_unset()
is the deprecated equivalent to doing$_SESSION = array()
. The latter andunset($_SESSION["..."])
are different only in the fact that theunset()
route will only unset a single session variable, the one named in[...]
. Never dounset($_SESSION)
, as that will interfere with the session mechanism itself.Old question reference.
唯一说
session_unset()
的是那些停留在过时版本 PHP 上的人 - 该函数已经被弃用很长时间了。这个问题的确切答案取决于您的代码究竟使用什么来确定某人是“登录”还是“注销”。
如果您的代码要查找单个
$_SESSION['logged_in'] = true
,那么为什么要取消设置它呢?只需将其设置为false
,然后“boom...”用户就会注销。The only ones saying
session_unset()
are the ones stuck on obsolete versions of PHP - the function's been deprecated for a LONG time now.The exact answer to this question depends on exactly what your code uses to determine if someone is "logged in" v.s. someone who is "logged out".
If you have a single
$_SESSION['logged_in'] = true
that your code looks for, then why unset it? Just set it tofalse
and boom... user is logged out.session_destroy — 销毁注册到会话的所有数据
session_unset — 释放所有会话变量
http://www.php.net/manual/en /book.session.php
我见过最常用的是按此顺序调用它们。
session_destroy — Destroys all data registered to a session
session_unset — Free all session variables
http://www.php.net/manual/en/book.session.php
The most I've seen used is to call them in this order.
如果您使用
session_destroy()
那么浏览器中的 cookie 也会被清除(并且可能稍后会创建一个新会话)我个人使用一个对象来跟踪不同的事物(例如
public loggingIn = False;
和一个真正让用户登录的函数)如果你想保留 coockie,session_unset() 很方便,但你最终会在服务器中得到更多的空会话
if you use
session_destroy()
then the cookie in the browser is also cleard (and probbley a new session gets created later)personaly i use an object(s) to track different things (like
public loggedIn = False;
and a function witch actally logs the user in)session_unset() is handy if you want to keep the coockie, but you will end up with more empty sessions in the server