4

We are trying to clear the cookie details in browser on pressing 'Logout' button with the following code, but the script doesn't remove the session cookie from the browser. But by clearing the session cookies in IE8 browser using developer tool(Tools > Developer Tools >Cache > Clear Session Cookies), the cookies are cleared.

<html:link page="/home.do" onclick="logout();">
    <html:img  page="/images/logout.jpg"/>
</html:link>

function logout(){
var cookies = document.cookie.split(";");     
for (var i = 0; i < cookies.length; i++)     {   
  var cookiename =  cookies[i].split("=");       
  var d = new Date();         
  d.setDate(d.getDate() - 4);   
  var expires = ";expires="+d;       
  var value="";       
  document.cookie = cookiename + "=" + value + expires + ";";     
  }
}

How to clear the Session cookies from the browser using script?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Uvi
  • 41
  • 1
  • 1
  • 2
  • Why do you want to remove the cookie? Call `session.invalidate()` would be enough, the cookie will be deleted on browser exit anyway. – f_puras Sep 07 '12 at 07:40
  • 1
    possible duplicate of [Clearing all cookies with JavaScript](http://stackoverflow.com/questions/179355/clearing-all-cookies-with-javascript) – tchrist Sep 09 '12 at 23:52

3 Answers3

3

if it is httpOnly, then you can not delete it, try modify it from server side

Sean Yang
  • 268
  • 1
  • 12
0

from javascript: Not specifying expiring field, also set value to null, and if it is also hostonly, not specifying domain

Alberto Perez
  • 1,019
  • 15
  • 17
-1

If it is a 'session' variable you want to clear...

unset( $_SESSION['YOUR_SESSION_VARIABLE_NAME'] );

This will completely remove this session variable. The title is misleading. The code he shows is not 'session' cookies, they are regular 'cookies'.

Tom aka Sparky
  • 76
  • 1
  • 11