php - 关于 session_destroy 的模糊性

标签 php session cookies destroy

我已经搜索了很多关于 session_destroy 到底是做什么的!但至少对我来说没有结果!首先阅读以下详细信息:

When a session is created (session_start) a file is created with a unique identifier that is given to the user as a cookie, when variables in the $_SESSION array are modified or added the temporary file is updated with that information so that it can be used somewhere else on the website.*

session_destroy* will delete this file, this is commonly done for when a user logs out of your website so that the (now useless and unnecessary) file isn't taking up space.

我们知道 session id 存储在 session cookie 中,正如教程所说, session 销毁会删除 session cookie 文件(包括 session_id ),所以为什么当我启动新 session 时它没有生成新的 id !这让我很困惑!看一下例子:

<?php
    session_start();
     echo session_id();
    session_destroy();
    session_start();

     echo "---".session_id();
?>

结果:l4k80dkrl5kd6cdlobhbu5s3i1---l4k80dkrl5kd6cdlobhbu5s3i1

所以它给我的 session ID 与前一个相同。

那么session_destroy到底是做什么的!! ?

提前致谢

最佳答案

来自 PHP 文档:

It does not unset any of the global variables associated with the session, or unset the session cookie.

因此,在 session_destroy() 之后,保存 session ID 的 cookie 仍然有效,只有 session 文件将被删除。所以 start_session() 尝试在 cookie 中查找 session id 的文件,当然失败了,它只是为此创建了一个新的空文件。所以你的 id 不会改变。

如果您确实想改变这一点,请尝试删除 cookie。

关于php - 关于 session_destroy 的模糊性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32371666/

相关文章:

node.js - 将 cookieSession 与 Passport 一起使用

jquery - 使用 jquery 创建 cookie

php - 在 PHP 中将 MySQL 记录集转换为 JSON 字符串

php - IF 规则 $_POST PHP - MYSQL

php - 如何在laravel中将数组中属性的JSON字符串值作为JSON数组返回

php - Symfony Doctrine 查询通过子查询计算组后的排名

php - 如何在php中循环遍历 session 数组

java - 如何避免创建 session ?

cookies - JWT + cookies + HTTPS + CSRF

javascript - 未捕获的类型错误 : cannot call method 'cookie' of undefined