PHP 中的 session_unset() 和 session_destroy() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4303311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:35:55  来源:igfitidea点击:

What is the difference between session_unset() and session_destroy() in PHP?

phpsessionsession-cookies

提问by Johan

From the php.netdocumentation:

来自php.net文档:

session_destroy— Destroys all data registered to a session

session_unset— Free all session variables

session_destroy— 销毁注册到会话的所有数据

session_unset— 释放所有会话变量

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?

这两个函数都不删除客户端的会话 cookie 是否正确?

回答by Gumbo

session_unsetjust clears the $_SESSIONvariable. It's equivalent to doing:

session_unset只是清除$_SESSION变量。这相当于做:

$_SESSION = array();

So this does only affect the local $_SESSIONvariable instance but not the session data in the session storage.

所以这只会影响局部$_SESSION变量实例,而不影响会话存储中的会话数据。

In contrast to that, session_destroydestroys the session data that is stored in the session storage (e.g. the session file in the file system).

与此相反,session_destroy销毁会话存储中存储的会话数据(例如文件系统中的会话文件)。

Everything else remains unchanged.

其他一切都保持不变。

回答by Xamael

session_destroy();is deleting the whole session.

session_destroy();正在删除整个会话。

session_unset();deletes only the variables from session - session still exists. Only data is truncated.

session_unset();仅从会话中删除变量 - 会话仍然存在。只有数据被截断。

回答by SLyHuy

session_unset();

Just clear all data of all session variable.

只需清除所有会话变量的所有数据。



session_destroy();

Remove all session.

删除所有会话。



Example示例

session_start();
session_destroy();     
$a = "1234";
$_SESSION[a] = $a;

$_SESSION[a]is NULL.

$_SESSION[a]NULL



session_start();
session_unset();     
$a = "1234";
$_SESSION[a] = $a;

$_SESSION[a]is 1234.

$_SESSION[a]1234



So, I will use:

所以,我将使用:

session_start();
session_destroy();   
session_start();  
$a = "1234";
$_SESSION[a] = $a;

回答by kaushik

session_unset()will clear the $_SESSIONvariable (as in array()), but it won't touch the session file. But when the script ends; the state of the $_SESSIONwill be written to the file. Then it will clear the file but won't delete it. When you use session_destroy()it won't touch $_SESSION(Use var_dump($_SESSION)after session_destroy()), but will delete the session file, so when script exits there won't be a file to write the state of the $_SESSION.

session_unset()将清除$_SESSION变量(如array()),但不会触及会话文件。但是当脚本结束时;的状态$_SESSION将写入文件。然后它会清除文件但不会删除它。当你使用session_destroy()它时不会接触$_SESSION(使用var_dump($_SESSION)之后session_destroy()),但会删除会话文件,所以当脚本退出时不会有一个文件来写入$_SESSION.

回答by Radian Yusuf Mahendra

session_destroy()will delete the session after moving the page and session_unset()will delete session when the code is run.

session_destroy()将在移动页面后 session_unset()删除会话,并在代码运行时删除会话。

回答by Marvin

I tried to use session_unset($_SESSION['session_name'])thinking it will only unset specific or individual/single session name. But using session_unset($_SESSION['session_name'])will only unset all session name. The right code to use is only unset($_SESSION['session_name'])if you want to unset a single session name.

我试图session_unset($_SESSION['session_name'])认为它只会取消设置特定或个人/单个会话名称。但是使用session_unset($_SESSION['session_name'])只会取消设置所有会话名称。仅unset($_SESSION['session_name'])当您想取消设置单个会话名称时才使用正确的代码。

回答by Yadab Sd

session_start(); #it will create an virtual array (associative) in browser realtime memory

session_start(); #它将在浏览器实时内存中创建一个虚拟数组(关联)

two item added

添加了两个项目

> $_SESSION['me'] = "Yadab";  
> $_SESSION['you'] = "Avi";
>
> print_r($_SESSION); #will give, array( "me"=>"Yadab", "you"=>"Avi" )

test1

测试1

> unset($_SESSION['me']); #only 'me' variable is removed fully (index & value) 
> print_r($_SESSION); #now the array is Array("you"=>"Avi")

test2

测试2

> session_destroy(); #will unset the values of all session variables, but indexes exists 
> print_r($_SESSION); #Output, Array("you"=>undefined)
> #but some browser can store the value in cookies

test3

测试3

> session_unset(); #will unset all the main variables not only the values 
> print_r($_SESSION); #that means session array is now empty, like Array()

test block 1, 2, or 3 at individually by comment out others

单独的测试块 1、2 或 3 通过注释掉其他

回答by Riaj Mahmud Rasel

I think session_destroy() and session_unset() should be used at the same time to make sure that session data is surely deleted.

我认为应该同时使用 session_destroy() 和 session_unset() 以确保会话数据确实被删除。