2

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

2
  • You want to kill the session, or to delete a session variable/key? 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.. Commented Nov 25, 2010 at 8:10

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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();
?>

3 Comments

Be aware that it's not guaranteed that your code will run. The user might remove it, a browser might decide not to run the code, ...
How do you mean that? If you declare it, for example, in the body onUnload="kill_session()" then actually every browser will run that..?
You have some things to learn about the client-server concept. Anything that is on the client side is under the control of the client and can be altered by him.
2

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

2 Comments

U mean, the session is saved in a cookie? This would mean that if you disabled cookies, you couldn't use any website which works with sessions..?
@Florian That is not correct. The ID of the session is most of the times saved in a cookie. The actual session data is saved on the server. But if you remove the cookie with the ID the client looses the session and that is what I think the asker wanted. The actual data will be removed by a garbage collector at a later time, but we shouldn't care about that because it can't be accessed without the ID.
1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.