最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to kill session from javascript - Stack Overflow

programmeradmin0浏览0评论

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.

thanks in advance

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.

thanks in advance

Share Improve this question asked Nov 25, 2010 at 7:46 ThomasThomas 34.2k129 gold badges372 silver badges642 bronze badges 2
  • You want to kill the session, or to delete a session variable/key? – Gideon Commented Nov 25, 2010 at 7:49
  • I think with the variable he wanted to describe what he's meaning. He wants to kill it completely.. – Florian Müller Commented Nov 25, 2010 at 8:10
Add a comment  | 

4 Answers 4

Reset to default 8

You need to tell the server to kill a session variable.

The only way to do that from javascript is to use Ajax to call some custom page, with for example as variable the session key you want to delete.

You have to fire an AJAX event, for example:

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:

<?php
    session_start();
    session_destroy();
?>

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.

See here for how to handle cookies from JavaScript: http://www.quirksmode.org/js/cookies.html

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/

发布评论

评论列表(0)

  1. 暂无评论