简体   繁体   English

Cookies不会删除

[英]cookies won't delete

Here is my login cookies being set 这是我设置的登录cookie

setcookie('username[0]',$username,time()+(60*60*24*365));
setcookie('username[1]',$userid,time()+(60*60*24*365));
setcookie('username[2]',$subscribed,time()+(60*60*24*365));         
setcookie('password',md5($password),time()+(60*60*24*365));
setcookie('admin',$admin,time()+(60*60*24*365));

Here is my logout function 这是我的注销功能

function logout($return) {

         setcookie('username[0]', '', time()-(60*60*24*365));
    setcookie('username[1]', '', time()-(60*60*24*365));
    setcookie('username[2]', '', time()-(60*60*24*365));
    setcookie('password', '', time()-(60*60*24*365));
    setcookie('admin', '', time()-(60*60*24*365));


      header( 'Location: ' . $return );

    echo "<div class='fontall'><span class='fontdif'>You've been logged out.  </span><a href='$return'>Click Here</a><span class='fontdif' to return</span></div>";

        }

When i try to log out and return to the page i am still logged in? 当我尝试注销并返回该页面时,我仍处于登录状态? What did i do wrong? 我做错了什么?

If you got the 'cannot modify headers' error, it means you echo out something before setcookie . 如果出现“无法修改标题”错误,则意味着您在setcookie之前回显了某些内容。 setcookie must do before any content echo out. 在任何内容回显之前, setcookie必须执行。

Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). 与其他标头一样,必须在脚本的任何输出之前发送cookie(这是协议限制)。 This requires that you place calls to this function prior to any output, including and tags as well as any whitespace. 这要求您在所有输出(包括和标记以及任何空格)之前都先调用此函数。

    // 1. Find the session
    session_start();

    // 2. Unset all the session variables
    $_SESSION = array();

    // 3. Destroy the session cookie
    if(isset($_COOKIE[session_name()])) {
        setcookie(session_name(), '', time()-42000, '/');
    }

    // 4. Destroy the session
    session_destroy();

That should work. 那应该工作。 Probably..u didn't destroy the session? 可能..u没有破坏会话?

Nothing seems to be wrong with the code - they should be deleting the cookies. 代码似乎没有错-他们应该删除Cookie。 Are you sure that the cookies are not deleting? 您确定cookie不会删除吗? After you logout, try checking if the cookies exist. 注销后,请尝试检查cookie是否存在。 You may do so using the browser that show the active cookies. 您可以使用显示活动cookie的浏览器来执行此操作。 Or alternatively you may try reading the cookies using PHP. 或者,您可以尝试使用PHP阅读cookie。

Second, how are you checking if the session is still valid? 其次,您如何检查会话是否仍然有效? Can you please share that piece of code? 您可以分享这段代码吗? And where do you check your session - do you do them on all pages? 您在哪里检查会话-您是否在所有页面上都进行会话?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM