这是销毁php中所有会话数据的正确方法吗? [英] Is this a proper way to destroy all session data in php?
问题描述
从php.net获得了它,但是我不确定这是否是每个人都破坏所有会话的方式吗?
Got it from php.net, but I am not sure is this how everybody destroy all sessions?
// Unset all Sessions
$_SESSION = array();
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time() -42000, '/');
}
session_destroy();
代码会破坏所有会话吗??这是最常见的方式吗?你们如何破坏php会话?
Does the code will destroy all the sessions?? Is it the most common way? how do you guys destroy php sessions?
哦,是的,session_name()
是什么?所有会话名称?例如$_SESSION['var1']
,$_SESSION['var2']
,...?
Oh yeah, btw, what is that session_name()
? All session name? e.g $_SESSION['var1']
, $_SESSION['var2']
, ... ?
我不再需要使用unset($_SESSION['var1']);
吗?
使用session_destroy()
和unset($_SESSION[])
有什么区别?
推荐答案
您首先应该知道会话是什么:您可以将会话视为与随机标识符(会话ID)相关联的服务器端数据容器.该会话ID需要由客户端提供,以便服务器可以将与该会话ID(并因此与该会话)关联的数据加载到$_SESSION
变量中.该$_SESSION
变量中的所有内容也称为当前活动会话的会话变量.
You should first know what sessions are: You can consider sessions as a data container on the server side that’s associated with a random identifier, the session ID. That session ID needs to be provided by the client so that the server can load the data associated to that session ID (and thus to that session) into the $_SESSION
variable. Everything in that $_SESSION
variable is also called session variables of the current active session.
现在您的问题是
代码会破坏所有会话吗??这是最常见的方式吗?你们如何破坏php会话?
Does the code will destroy all the sessions?? Is it the most common way? how do you guys destroy php sessions??
提供的代码仅删除当前会话的会话数据. $_SESSION = array();
语句将仅重置会话变量$_SESSION
,以便将来对会话变量$_SESSION
的访问将失败.但是会话容器本身尚未删除.可以通过调用session_destroy
来完成.
The provided code just deletes the session data of the current session. The $_SESSION = array();
statement will simply reset the session variable $_SESSION
so that a future access on the session variable $_SESSION
will fail. But the session container itself is not deleted yet. That will be done by calling session_destroy
.
另请参阅 要真正销毁PHP会话吗?
哦是的,顺便说一句,session_name()是什么?所有会话名称?例如$ _SESSION ['var1'],$ _ SESSION ['var2'] ...?
Oh yeah, btw, what is that session_name()?? All session name? e.g $_SESSION['var1'], $_SESSION['var2']... ?
session_name 仅用于标识在Cookie(URL查询)中传递的会话ID参数或通过POST参数. PHP的默认值为PHPSESSID
.但是您可以将其更改为所需的任何内容.
The session_name is just used to identify the session ID parameter passed in a cookie, the URL’s query or via a POST parameter. PHP’s default value is PHPSESSID
. But you can change it to whatever you want to.
我不需要使用unset($ _ SESSION ['var1']);还有吧?
I dont need to use unset($_SESSION['var1']); any more right???
不.开头的$_SESSION = array();
删除所有会话数据.
No. The initial $_SESSION = array();
deletes all the session data.
使用session_destroy和unset($ _ SESSION [])有何区别?
Whats the different between using session_destroy and unset($_SESSION[])??
session_destroy
将在unset
或重置$_SESSION
时删除整个会话容器.变量只会删除当前运行时的会话数据.
session_destroy
will delete the whole session container while unset
or resetting the $_SESSION
variable will only delete the session data for the current runtime.
这篇关于这是销毁php中所有会话数据的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!