Cookie不会删除 [英] Cookie Won't Delete

查看:117
本文介绍了Cookie不会删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个代码在我的index.php:

 <?php if(isset($ _ POST [' cookie'])){setcookie(RememberMe,Yes,time()+ 1209600); }?> 

如果用户选中了Remember me框,那么它将设置一个名为RememberMe for 2的Cookie周。这部分工作正常。



现在我遇到的问题是,当他们按下注销时删除这个cookie。



按下注销时,它们被重定向到logout.php,它有以下代码:

 <?php include_once .php'); 
include_once('functions.php');

unset($ _ COOKIE ['RememberMe']);
setcookie(RememberMe,,time() - 3600);
$ _SESSION = array(); session_destroy();


?>

< meta http-equiv =refreshcontent =0; ../ index.php>

但由于某些奇怪的原因,不会删除cookie?任何想法为什么?

解决方案

您可能想检查cookie的路径设置是否正确。默认情况下,PHP将Cookie路径设置为它设置的目录,它不会从不同的位置可用(也不可能删除)。



p>


  • 无需取消设置 $ _ COOKIE $ _ SESSION

     >  header('Location:/index.php'); //或任何您要重定向到
    的路径



So I have this code on my index.php:

<?php if(isset($_POST['cookie'])) { setcookie("RememberMe", "Yes", time()+1209600);  } ?>

If the user has checked the remember me box then it will set a cookie with the name RememberMe for 2 weeks. This part works fine.

Now the issue I'm having is deleting this cookie when they press logout.

On pressing logout, they get redirected to logout.php which has the following code:

<?php include_once('config.php');
include_once('functions.php');

unset($_COOKIE['RememberMe']);
setcookie("RememberMe", "", time()-3600);
$_SESSION = array(); session_destroy(); 


?> 

<meta http-equiv="refresh" content="0;../index.php"> 

but for some strange reason that won't delete the cookie? Any ideas as to why?

解决方案

You may want to check if the path the cookie is set at is correct. By default PHP sets the cookie path to the directory it's set in and it will not be available (nor possible to delete) from different locations.

Few more tips:

  • there is no need to unset $_COOKIE and $_SESSION
  • instead of redirecting using a meta tag redirect with HTTP headers:

    header('Location: /index.php'); // or whatever is the path you want to redirect to
    

这篇关于Cookie不会删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆