在 PHP 中 session_unset()和 session_delete()的区别是什么?

来自 Php.net文档:

Session _ delete ーー销毁注册到会话的所有数据

Session _ unset ーー释放所有会话变量

我的三个问题是:

这两个函数看起来非常相似。
这两者之间真正的区别是什么?

两者似乎都删除了注册到会话的所有变量。他们中有人真的破坏了会议本身吗?如果没有,您如何实现这一点(销毁会话本身)。

这两个函数都没有删除客户端上的会话 cookie,对吗?

69064 次浏览

session_destroy(); is deleting the whole session.

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

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.

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;

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.

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() will delete the session after moving the page and session_unset() will delete session when the code is run.

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_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