Javascript 如何删除会话cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12313790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:42:07  来源:igfitidea点击:

How to remove session cookie

javascriptcookiessession-cookies

提问by Uvi

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.

我们正在尝试使用以下代码在按下“注销”按钮时清除浏览器中的 cookie 详细信息,但脚本不会从浏览器中删除会话 cookie。但是通过使用开发者工具(工具 > 开发者工具 > 缓存 > 清除会话 Cookies)清除 IE8 浏览器中的会话 cookie,cookie 被清除。

<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?

如何使用脚本从浏览器中清除会话 cookie?

回答by Sean Yang

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

如果它是httpOnly,那么你不能删除它,尝试从服务器端修改它

回答by Sparky aka tom

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'.

这将完全删除此会话变量。标题有误导性。他展示的代码不是“会话”cookie,它们是常规的“cookie”。