duanfen7676 2012-08-19 19:32
浏览 63
已采纳

注销时不会删除Cookie

I've got a problem, user can't Log Out because the $_COOKIE's are not actually deleting. I can't find out what could be the problem.

This code is used only once at Log In:

// Log In
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['username'] = $row['username'];
setcookie('user_id', $row['user_id'], time() + 2592000);
setcookie('username', $row['username'], time() + 2592000);

The code below is checking if cookies are set up to make users to be logged in when they relaunch their browser (the "keep me logged in" effect).

// Starting Session
session_start();
// If the session vars aren't set, try to set them with cookies
if (!isset($_SESSION['user_id'])) {
    // This check always equals true because cookies are not deleting on Log Out
    if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
        $_SESSION['user_id'] = $_COOKIE['user_id'];
        $_SESSION['username'] = $_COOKIE['username'];
    }
}

This code is launched only once on Log Out:

// Log Out
session_start();
if (isset($_SESSION['user_id'])) {
    $_SESSION = array();
    if (isset($_COOKIE[session_name()])) {
        setcookie(session_name(), '', time() - 2592000, '/');
    }
    session_destroy();
}
setcookie('user_id', '', time() - 2592000);
setcookie('username', '', time() - 2592000);
  • 写回答

3条回答 默认 最新

  • duan010167787 2013-02-03 17:50
    关注

    I found why cookies were not removing!

    To make sure your cookies will remove, set the same path on removing cookies as on setting them.

    // Setting Cookie
    setcookie(session_name(), '', time()-2592000, '/'); // The path here is "/"
    
    // Removing Cookie
    setcookie(session_name(), '', time()+2592000, '/'); // The path here is "/"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了