我已经阅读了很多关于注销脚本的php教程,我想知道什么是从会话注销的正确方法!
脚本1
<?php
session_start();
session_destroy();
header("location:index.php");
?>
Run Code Online (Sandbox Code Playgroud)
脚本2
<?php
session_start();
session_unset();
session_destroy();
header("location:index.php");
?>
Run Code Online (Sandbox Code Playgroud)
脚本3
<?php
session_start();
if (isset($_SESSION['username']))
{
unset($_SESSION['username']);
}
header("location:index.php");
?>
Run Code Online (Sandbox Code Playgroud)
有没有更有效的方法来做到这一点?可以通过重新登录来创建会话,所以我是否应该使用session_destroy()并使用unset($ _ SESSION ['variable'])来代替?上面哪3个脚本更优选?
Frx*_*rem 61
<?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();
?>
Run Code Online (Sandbox Code Playgroud)
irc*_*ell 11
就个人而言,我做了以下事情:
session_start();
setcookie(session_name(), '', 100);
session_unset();
session_destroy();
$_SESSION = array();
Run Code Online (Sandbox Code Playgroud)
这样,它就会杀死cookie,破坏内部存储的所有数据,并销毁会话信息的当前实例(被忽略session_destroy).
Session_unset();只会破坏会话变量。要结束会话session_destroy();,还调用了另一个函数,该函数也会破坏会话。
更新 :
为了完全终止会话,比如注销用户,会话 ID 也必须取消设置。如果使用 cookie 来传播会话 ID(默认行为),则必须删除会话 cookie。setcookie()可以用于
<?php
// Initialize the session.
session_start();
// Unset all of the session variables.
unset($_SESSION['username']);
// Finally, destroy the session.
session_destroy();
// Include URL for Login page to login again.
header("Location: login.php");
exit;
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85245 次 |
| 最近记录: |