小菜鸟再认识session_destroy()

在实现用户退出功能时,遇到session数据无法立即删除的问题。通过研究发现,session_destroy()仅销毁当前会话的数据,不清除全局变量或会话cookie。要重新使用会话变量,需要再次调用session_start()。为立即删除session变量,可以使用unset或设置变量为空值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/** 燕十八 公益PHP培训

 课堂地址:YY频道88354001

 学习社区:www.zixue.it **/

前几天在做一个用户退出功能时出现了一点小问题,session中的信息不能第一时间删除,直接上代码

<?php
//logout.php
define('PASS',true);
require_once './include/init.php';
unset($_SESSION);
session_destroy();
$msg='用户退出成功';
include ROOT.'view/front/msg.html';


?>

第一次刷新时,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_destroy不能删除客户端保存的关于session的变量,和$_SESSION中的变量,它只是关闭或者毁坏了session的连接。

所以,想要清楚session的变量,可以用unset或者讲变量设为空值

改成下面之后,一运行logout.php页面session中的信息就会被删除了

<?php
define('PASS',true);
require_once './include/init.php';
unset($_SESSION);
$msg='用户退出成功';
include ROOT.'view/front/msg.html';


?>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值