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

发布于 2024-10-05 03:54:39 字数 539 浏览 3 评论 0原文

来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

七度光 2024-10-12 03:54:40

我尝试使用 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 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.

猫瑾少女 2024-10-12 03:54:40

session_destroy() 将在移动页面后删除会话

session_unset() 将在代码运行时删除会话。

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

猫弦 2024-10-12 03:54:40

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

通过注释掉其他项分别添加两个项目

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

test1

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

test2

> 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

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

测试块1、2或3

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

two item added

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

test1

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

test2

> 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

> 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

青柠芒果 2024-10-12 03:54:40

我认为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.

阿楠 2024-10-12 03:54:39

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

$_SESSION = array();

所以这只会影响本地 $_SESSION 变量实例,但不会影响会话存储中的会话数据。

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

其他一切保持不变。

session_unset just clears the $_SESSION variable. It’s equivalent to doing:

$_SESSION = array();

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.

好菇凉咱不稀罕他 2024-10-12 03:54:39

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.

乖不如嘢 2024-10-12 03:54:39
session_unset();

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

session_destroy();

删除所有会话。


Example:

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

$_SESSION[a]NULL

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

$_SESSION[a]1234

所以,我将使用:

session_start();
session_destroy();   
session_start();  
$a = "1234";
$_SESSION[a] = $a;
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_start();
session_unset();     
$a = "1234";
$_SESSION[a] = $a;

$_SESSION[a] is 1234.

So, I will use:

session_start();
session_destroy();   
session_start();  
$a = "1234";
$_SESSION[a] = $a;
不一样的天空 2024-10-12 03:54:39

session_unset() 将清除 $_SESSION 变量(如 array() 中所示),但不会触及会话文件。但当剧本结束时; $_SESSION 的状态将被写入该文件。然后它会清除该文件但不会删除它。当您使用 session_destroy() 时,它不会触及 $_SESSION (在 session_destroy() 之后使用 var_dump($_SESSION) code>),但会删除会话文件,因此当脚本退出时,不会有文件来写入 $_SESSION 的状态。

session_unset() will clear the $_SESSION variable (as in array()), 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文