PHP 中的 session_unset() 和 session_destroy() 有什么区别?
来自 php.net 文档:
session_destroy — 销毁所有数据注册到会话
session_unset — 释放所有会话变量
My三部分问题是:
这两个功能看起来非常相似。
两者之间到底有什么区别?
两者似乎都会删除注册到会话的所有变量。它们中的任何一个实际上会破坏会话本身吗?如果没有,您如何完成此操作(销毁会话本身)。
这两个函数都不会删除客户端的会话 cookie,这是否正确?
From the php.net documentation:
session_destroy — Destroys all data registered to a session
session_unset — Free all session variables
My three part question is:
The two functions seem very similar.
What is really the difference between the two?
Both seem to delete all variables registered to a session. Does any of them actually destroy the session itself? If not, how do you accomplish this (destroy the session itself).
Is it correct that neither of the two functions deletes the session cookie at the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我尝试使用
session_unset($_SESSION['session_name'])
认为它只会取消设置特定或单个/单个会话名称。但使用session_unset($_SESSION['session_name'])
只会取消设置所有会话名称。如果您想取消设置单个会话名称,则仅使用unset($_SESSION['session_name'])
才是正确的代码。I tried to use
session_unset($_SESSION['session_name'])
thinking it will only unset specific or individual/single session name. But usingsession_unset($_SESSION['session_name'])
will only unset all session name. The right code to use is onlyunset($_SESSION['session_name'])
if you want to unset a single session name.session_destroy()
将在移动页面后删除会话和
session_unset()
将在代码运行时删除会话。session_destroy()
will delete the session after moving the pageand
session_unset()
will delete session when the code is run.会话开始(); #它将在浏览器实时内存中创建一个虚拟数组(关联),
通过注释掉其他项分别添加两个项目
test1
test2
test3
测试块1、2或3
session_start(); #it will create an virtual array (associative) in browser realtime memory
two item added
test1
test2
test3
test block 1, 2, or 3 at individually by comment out others
我认为session_destroy()和session_unset()应该同时使用,以确保会话数据确实被删除。
I think session_destroy() and session_unset() should be used at the same time to make sure that session data is surely deleted.
session_unset
只是清除$_SESSION变量。这相当于:
所以这只会影响本地
$_SESSION
变量实例,但不会影响会话存储中的会话数据。与此相反,
session_destroy
会销毁会话数据存储在会话存储器中(例如文件系统中的会话文件)。其他一切保持不变。
session_unset
just clears the$_SESSION
variable. It’s equivalent to doing:So this does only affect the local
$_SESSION
variable instance but not the session data in the session storage.In contrast to that,
session_destroy
destroys the session data that is stored in the session storage (e.g. the session file in the file system).Everything else remains unchanged.
session_destroy();
正在删除整个会话。session_unset();
仅删除会话中的变量 - 会话仍然存在。仅数据被截断。session_destroy();
is deleting the whole session.session_unset();
deletes only the variables from session - session still exists. Only data is truncated.Example:
所以,我将使用:
Example:
So, I will use:
session_unset()
将清除$_SESSION
变量(如array()
中所示),但不会触及会话文件。但当剧本结束时;$_SESSION
的状态将被写入该文件。然后它会清除该文件但不会删除它。当您使用session_destroy()
时,它不会触及$_SESSION
(在session_destroy()
之后使用var_dump($_SESSION)
code>),但会删除会话文件,因此当脚本退出时,不会有文件来写入$_SESSION
的状态。session_unset()
will clear the$_SESSION
variable (as inarray()
), but it won't touch the session file. But when the script ends; the state of the$_SESSION
will be written to the file. Then it will clear the file but won't delete it. When you usesession_destroy()
it won't touch$_SESSION
(Usevar_dump($_SESSION)
aftersession_destroy()
), but will delete the session file, so when script exits there won't be a file to write the state of the$_SESSION
.