这是我的剧本:
<?php
// If the user is logged in, delete the session vars to log them out
session_start();
if (isset($_SESSION['user_id'])) {
// Delete the session vars by clearing the $_SESSION array
$_SESSION = array();
// Delete the session cookie by setting its expiration to an hour ago (3600)
if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time() - 3600); }
// Destroy the session
session_destroy();
}
// Delete the user ID and username cookies by setting their expirations to an hour ago (3600)
setcookie('user_id', '', time() - 3600);
setcookie('username', '', time() - 3600);
// Redirect to the home page
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
header('Location: ' . $home_url);
?>
登录网站后我无法注销。我真的需要cookie登录吗?或者我可以把它拿出来吗?
答案 0 :(得分:14)
尝试更简单的方法,销毁所有会话cookie
session_start();
session_destroy();
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php';
header('Location: ' . $home_url);
答案 1 :(得分:1)
我建议使用此方法,
<?php
//User session in ['user']
if($_SESSION['user_id']){
session_start();
session_unset();
session_destroy();
session_write_close();
setcookie(session_name(),'',0,'/');
session_regenerate_id(true);
}
?>
我建议您使用该方法
,为什么?因为该方法使用PHP中的true destroy
,delete cookie in browser
和new set ID of session
会话
答案 2 :(得分:0)
“为了完全终止会话,要将用户注销,还必须取消设置会话ID。如果使用cookie传播会话ID(默认行为),则必须删除会话cookie。 setcookie()可能会用于此。“ 见:http://us1.php.net/session_destroy 并且:http://us1.php.net/manual/en/function.session-id.php “警告 不要使用unset($ _ SESSION)取消整个$ _SESSION,因为这将禁用通过$ _SESSION超全局注册会话变量。“ 见:http://us2.php.net/manual/en/function.session-unset.php