如何在PHP中取消设置cookie

如何在PHP中取消设置cookie

本文介绍了如何在PHP中取消设置cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要弄清楚如何取消设置此Cookie。我试过的一切都失败了。

i need to figure out how to unset this cookie. everything i have tried has failed so far.

这就是我如何目前你重置它,它似乎没有工作。

this is how i am currently unsetting it, and it doesnt seem to work.

setcookie("user_id",$user_id,time()-7200);

这是我如何设置

setcookie("user_id",$user_id,time()+7200);

我有一个名为set_session_from_cookie()的函数,用于检查是否设置了cookie, ,它使用cookie启动一个新的会话。

i have this function called set_session_from_cookie() that checks if a cookie is set, and if it is set, it starts a new session using the cookie.

问题是,当我在我的网页上使用这个。我无法注销,我认为这是因为我不能取消设置会话。

The problem, is that when i use this on my page. I am unable to logout, i assume this is because i am not unable to unset the session.

我有这个功能的原因是,如果用户想要记住在他们结束会话后,他们可以通过调用cookie重新启动会话。

the reason i have this function, is if a user wants to be remembered after they end the session, they can restart the session by calling the cookie.

function set_session_from_cookie(){

    if(isset($_SESSION['user_id'])){
    echo'';
}else{
    $_SESSION['user_id']=$_COOKIE['user_id'];

}
}

登出

<?php
require'core.php';
session_destroy();


setcookie("user_id","",time()-7200);
header('Location:/social_learning/site_pages/starter-template.php');

?>

我用下面的代码设置我的cookie。

i set my cookie with the following code.

if($rememberme=="on"){
                                            $user_id=mysql_result($query_run,0,'id');
                                            setcookie("user_id",$user_id,time()+7200);
                                            $_SESSION['user_id']=$user_id;
                                            redirect('home_page.php');
}else if($rememberme==""){
                                            echo'ok';
                                            $user_id=mysql_result($query_run,0,'id');
                                            echo $user_id;
                                            $_SESSION['user_id']=$user_id;
                                            redirect('home_page.php');
}

如何使用保存的cookie重新启动会话,而不使用函数i创建?因为该函数似乎导致我无法注销。

how can i restart the session using the saved cookie, without using the function i created? since the function seems to be causing my to no longer be able to logout.

推荐答案

这个问题的解决方案是,需要设置正确的路径来取消设置cookie,因为我是从一个不同的文件,我最初设置它。

The solution to this problem was that the I needed to set the correct path to unset the cookie since i was unsetting it from a different file that i originally set it in.

我找到了我需要使用的路径通过查找我的浏览器cookie内的cookie取消设置,一旦我在浏览器中找到cookie,该路径就被列在cookie附近。所以我设置的路径,像这样

I found out which path I needed to use for the unset by looking for the cookie inside my browser cookies, and once i found the cookie inside my browser, the path was listed near the cookie. so i then set the path to the cookie like so

setcookie("user_id",$user_id,time()-1,"/social_learning/site_pages");

最后一个参数是路径。这工作。

the last parameter is the path. and this worked.

我的原始设置Cookie看起来像这样。

my original set cookie looks like this.

setcookie("user_id",$user_id,time()+7200,"");

这篇关于如何在PHP中取消设置cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:34