admin管理员组

文章数量:1353127

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?

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?

Share Improve this question edited Sep 7, 2012 at 7:48 CharlesB 90.6k29 gold badges201 silver badges228 bronze badges asked Sep 7, 2012 at 7:37 UviUvi 411 gold badge1 silver badge2 bronze badges 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 Commented Sep 7, 2012 at 7:40
  • 1 possible duplicate of Clearing all cookies with JavaScript – tchrist Commented Sep 9, 2012 at 23:52
Add a ment  | 

3 Answers 3

Reset to default 3

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

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

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

unset( $_SESSION['YOUR_SESSION_VARIABLE_NAME'] );

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

本文标签: javascriptHow to remove session cookieStack Overflow