1

is it possible to rewrite the value of stored cookie? If so How to do that?

My exact case is,
I have created a cookie with some PATH(frm user,which i don know probably). I have to rewrite the value of this cookie somewhere else. I can just create a cookie wit same name but the problem here is i don know the PATH value.

  1. Either i should get the PATH of the stored cookie..
  2. else, i should re-write the existing cookie

Any help would be most welcome Thanks.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Jeevi
  • 2,962
  • 6
  • 39
  • 60

2 Answers2

1

you can save cookie like that :

function setCookie(name, value, expires) {  
document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString()); 
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
1

From the DOC

If the user agent receives a new cookie with the same cookie-name, domain-value, and path-value as a cookie that it has already stored,the existing cookie is evicted and replaced with the new cookie. Notice that servers can delete cookies by sending the user agent a new cookie with an Expires attribute with a value in the past.

So, What @Royi Namir has posted is the solution. Coz there is no specific way to rewrite cookie other than setting it again.

Refer this answer in SO regarding getting path info of a stored cookie

Community
  • 1
  • 1
Tamil
  • 5,260
  • 9
  • 40
  • 61
  • Hi. thanks for ur reply.. if i'm not going to giv the same path.. The existing cookie will not be overriden rite? – Jeevi Apr 30 '12 at 12:29
  • Again From the DOC:`If the server omits the Path attribute, the user agent will use the "directory" of the request-uri's path component as the default value`. So if the cookie you wish to delete takes the same path as the default value you might get a chance. – Tamil Apr 30 '12 at 13:42