March 20, 2012

How to clear the session using a button and javascript

Question by user782104

Session is client side staff, but is it possible through clear it using javascript code?
I would like too have the idea like this and ,can it convert to jquery format? Thank you.

js

    $(function(){
  $("#closeTab").click(function() {
    window.parent.$('#tt').tabs('close','Create List');
        $.post("clear.php",function(data){
      });
  });
});

php

    <?
if (isset($_SESSION['lname']))
unset($_SESSION['lname']);
if (isset($_POST['creminder']))
unset($_SESSION['creminder']);
?>

Is this one ok ?

Answer by Shyju

make an ajax call to the server and let your server page kills/ends the session

HTML

<a href="#" id="aKill" > Kill Session</a>

Script

$(function(){

  $("#aKill").click(function(){
        $.post("serverpage.php",function(data){
        // if you want you can show some message to user here
     });
});

and in your serverpage.php, Execute the PHP script to terminate the session.

Answer by Starx

Create a separate file to clear the session only. clearsession.php

session_start();
session_destroy();

Now, make a simple request

$("#aKill").click(function(){
    $.get("clearsession.php");
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!