从PHP中的会话注销的正确方法 [英] proper way to logout from a session in PHP

查看:106
本文介绍了从PHP中的会话注销的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have read many php tutorials for logout scripts, i am wondering what could be the proper way to logout from a 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_destroy()而使用unset($ _ SESSION ['variable'])代替吗?以上3个脚本中的哪一个更可取?

Is there any more effective way to do this?? A session can always be created by logging back in, so should i bother about use of session_destroy() and use unset($_SESSION['variable']) instead? which one of the above 3 script is more preferable?

推荐答案

session_destroy()页中 PHP手册:

<?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中的会话注销的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆