Javascript 如何从javascript终止会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4274724/
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
how to kill session from javascript
提问by Thomas
many time we use session variable to store data in page. i need way out to kill session from JavaScript when user will jump from one page to another page. is it possible. if yes then please guide me.
很多时候我们使用会话变量在页面中存储数据。当用户从一个页面跳转到另一个页面时,我需要从 JavaScript 中终止会话。是否可以。如果是,那么请指导我。
thanks in advance
提前致谢
回答by Gidon
You need to tell the server to kill a session variable.
您需要告诉服务器终止会话变量。
The only way to do that is to that from javascript is to use Ajax to call some custom page, with for example as variable the session key you want to delete.
做到这一点的唯一方法是从 javascript 中使用 Ajax 调用一些自定义页面,例如将要删除的会话密钥作为变量。
回答by Florian Müller
You have to fire an AJAX event, for example:
你必须触发一个 AJAX 事件,例如:
function kill_session() {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","session_destroyer.php",false);
xmlhttp.send();
document.getElementById("id_of_a_hidden_div").innerHTML=xmlhttp.responseText;
}
And your session_destroyer.php might looks like:
你的 session_destroyer.php 可能看起来像:
<?php
session_start();
session_destroy();
?>
回答by Alin Purcaru
Remove the session cookie. For PHP it's called PHPSESSID. If you do this the browser will loose the session ID and the actual session data will no longer be accessible for that client.
删除会话cookie。对于 PHP,它称为 PHPSESSID。如果您这样做,浏览器将丢失会话 ID,并且该客户端将无法再访问实际的会话数据。
See here for how to handle cookies from JavaScript: http://www.quirksmode.org/js/cookies.html
请参阅此处了解如何处理来自 JavaScript 的 cookie:http: //www.quirksmode.org/js/cookies.html
回答by Ghyath Serhal
Session object is server object, you cannot access it from the javascript directly. you should create an ajax call to the server in order to kill the session. you can use jquery to do that, very easy, check this link. http://api.jquery.com/jQuery.ajax/
Session 对象是服务器对象,您不能直接从 javascript 访问它。您应该创建对服务器的 ajax 调用以终止会话。您可以使用 jquery 来做到这一点,非常简单,请查看此链接。 http://api.jquery.com/jQuery.ajax/