gpt4 book ai didi

php - 从 PHP 中注销 session 的正确方法

转载 作者:IT王子 更新时间:2023-10-29 01:09:40 24 4
gpt4 key购买 nike

我已经阅读了许多有关注销脚本的 php 教程,我想知道从 session 中注销的正确方法是什么!

脚本 1

<?php
session_start();
session_destroy();
header("location:index.php");
?>

脚本 2

<?php
session_start();
session_unset();
session_destroy();
header("location:index.php");
?>

脚本 3

<?php
session_start();
if (isset($_SESSION['username']))
{
unset($_SESSION['username']);
}
header("location:index.php");
?>

有没有更有效的方法来做到这一点?总是可以通过重新登录来创建 session ,所以我应该为使用 session_destroy() 而烦恼,而是使用 unset($_SESSION['variable']) 吗?以上 3 个脚本中哪个更可取?

最佳答案

来自session_destroy() PHP manual 中的页面:

<?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();
?>

关于php - 从 PHP 中注销 session 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3512507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com