php手册写道
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.
In order to kill the session altogether, like to log the user out, the session id must also be unset. If a cookie is used to propagate the session id (default behavior), then the session cookie must be deleted. setcookie() may be used for that.
百度知道 写道
session 是一个会话,在session_start() 的时候建立。
$_SESSION 是一个全局数组,和其他的全局数组没有任何区别。只是php在创建一个会话的时候,会顺便创建一个全局数组来保存会话的内容,这个数组又刚好叫$_SESSION,和我们自己去创建的其他数组没有什么不一样。
session_start() 执行的时候,开启了会话,首先是如果该会话没有被创建,则在系统的tmp目录中创建了session文件(默认,可自定义),并且在当前会话的执行脚本创建全局数组$_SESSION,h这时的$_SESSION是一个空数组。如果会话存在,则读取session文件中的内容,建立$_SESSION数组。
session_destroy() ,手册中明确的说道:destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.
不会删除 session 的全局变量和 session相关cookie,只是结束了这次会话。作为一个普普通通的全局变量,如果我们不需要乐得话,就将他 unset() 掉。如果想再次开启会话,就需要再一次session_start(),但是session_start() 重新建立会话,会重新初始化$_SESSION数组,session_start()之后$_SESSION 就又是一个空数组了。
我们查看服务器端session.save_path目录会发现很多类似sess_vv9lpgf0nmkurgvkba1vbvj915这样的文件,这个其实就是session id “vv9lpgf0nmkurgvkba1vbvj915″对应的数据。真相就在这里,客户端将session id传递到服务器,服务器根据session id找到对应的文件,读取的时候对文件内容进行反序列化就得到session的值,保存的时候先序列化再写入。
其实还有很多中存储session的方式,可以通过php -i|grep “Registered save handlers”查看,比如Registered save handlers => files user sqlite eaccelerator可以通过文件、用户、sqlite、eaccelerator来存,如果服务器装了memcached,还有会mmcache的选项。当然还有很多,比如MySQL、PostgreSQL等等。都是不错的选择。
session的同步
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
?>