JS cannot clear the cookie solution

Source: Internet
Author: User
Tags php write setcookie

When you use Sdmenu.js in your project, you need to clear the cookie when you log on, and Sdmenu will save the cookie by default.

Here's how sdmenu.js saves cookies

Document.cookie = "Sdmenu_" + encodeuricomponent (this.menu.id) + "=" + States.join ("") + "; Expires= "+ d.togmtstring () +"; path=/";

Here's what I'm looking for on the internet. The way to clear cookies is always impossible to delete cookies

1. Pass the cookie name

function Deletecookie (name) {

var date=new date ();

Date.settime (Date.gettime ()-10000);

Document.cookie=name+ "=V; Expire= "+date.togmtstring () +"; path=/";

}

2. Circular deletion of all cookies

JS Traversal All Cookies

function foreach ()

{

var Strcookie=document.cookie;

var arrcookie=strcookie.split (";"); Cutting multi-Cookie into multiple name/value pairs

for (var i=0;i <arrcookie.length;i++)//Traversal cookie array, processing each cookie on

{var arr=arrcookie[i].split ("=");

if (arr.length>0)

Delcookie (Arr[0]);

}

}

function Getcooki (offset)

{var endstr = document.cookie.indexOf (";", offset);

if (endstr = =-1)

Endstr = Document.cookie.length;

Return decodeURIComponent (document.cookie.substring (offset, endstr));

}

function Delcookie (name)

{

var exp = new Date ();

Exp.settime (Exp.gettime ()-1);

var cval = GetCookie (name);

Document.cookie = name + "=" + Cval + "; Expires= "+ exp.togmtstring ();

}
function GetCookie (name)

{var arg = name + ' = ';

var alen = Arg.length;

var clen = document.cookie.length;

var i = 0; while (I < Clen)

{var J = i + Alen;

if (Document.cookie.substring (i, j) = = arg)

Return Getcooki (j);

i = Document.cookie.indexOf ("", I) + 1;

if (i = = 0)

Break

}

return null;

}

This web site was later found to explain why cookies could not be deleted.

http://php.js.cn/blog/delete-cookie/

There is a problem with a cookie today: a cookie named username, whose domain is www.umiwi.com, cannot be deleted, causing the user to log out later. After a long day finally found the problem: it is my basic knowledge of the cookie is not clear ....

Cookies do not only have names and values of two attributes, but also domains (domain), expiration time (expires), path, and so on. A cookie with the same name can exist in different domains and different paths. Like this page: Poke me with the mouse

    

In addition, I found that when setting a cookie, if no domain name is specified, the domain of this cookie is implicitly considered the current domain, such as www.umiwi.com. If you specify a domain when you set it, the browser will automatically add it before it is saved .  such as PHP code: setcookie (' Test ', ' a ', 0, ' www.umiwi.com '); Then the domain of the cookies stored in the browser is . www.umiwi.com.  So, the best way to do this is to set the cookie, how to write it, and how to write it when you delete it. Or there will be the situation I encountered: clearly browser see Username=longbill, domain for www.umiwi.com cookie, but in PHP write setcookie (' username ', ', Time ()- ' www.umiwi.com ', '/'); The cookie cannot be deleted. The reason is that I deleted the cookie in fact, the operation is a new name username, the value is empty, the expiration time of the past 1000 seconds, the domain is . www.umiwi.com, the path is a cookie. This cookie is immediately expired in the browser and cannot be seen. And I'm going to delete the cookie on the http://www.umiwi.com/that's alive and well ...

There is no add path (path) in the code that deleted the cookie.

Original:

Document.cookie = "Sdmenu_" + encodeuricomponent (this.menu.id) + "=" + States.join ("") + "; Expires= "+ d.togmtstring () +"; path=/";

New:

var date=new date ();           Date.settime (Date.gettime ()-10000); Document.cookie=name+ "=V; Expire= "+date.togmtstring() +"; path=/";

In addition to the path, there may be other situations such as domains, insurance is how to set the cookie style, what should be deleted when the style, plus the Expire property on the line

JS cannot clear the cookie solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.