Thomas
Thomas

Reputation: 34188

how to kill session from javascript

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

Upvotes: 2

Views: 40050

Answers (4)

Gideon
Gideon

Reputation: 18491

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.

Upvotes: 8

Alin P.
Alin P.

Reputation: 44346

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

Upvotes: 2

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

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/

Upvotes: 1

Florian Müller
Florian Müller

Reputation: 7785

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

Upvotes: 2

Related Questions