4

Button on index page:

$('#killsession').click(function() {
    $.get('killsession.php');
    alert("OK");
});

killsession.php:

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

After killing the session with this button, any session-esque related functions on index still work (session variables are still set/exist). For example, I have a counting session variable that is incremented when I click a certain button. This counting variable does not lose its spot in counting after killing the session.

Is it possible to kill a session with a JQuery button?

3 Answers 3

5

All the PHP session items are loaded when the page is first loaded. They are still in the page/browser memory as long as the page is open. You need to reload the page after killing the session. You can do this with javascript window.location.href = window.location.href

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

Comments

4
$('#killsession').click(function() {
  $.get('killsession.php', function() {
    alert("the server page executed");
    //Here you may do further things.

    window.location = window.location;
  });
});

killsession.php

session_start();

$_SESSION = array();

$params = session_get_cookie_params();

setcookie( session_name(), '', time() - 42000,
    $params["path"],
    $params["domain"],
    $params["secure"],
    $params["httponly"]
);

session_destroy();

exit('OK');

Comments

1

Make sure you are doing other things (like checking session) only inside the call back of ajax function .Whatever inside the callback will be executed after receiving a response from the ajax server page.

$('#killsession').click(function() {
    $.get('killsession.php',function(){
       alert("the server page executed");
       //Here you may do further things.

    });

});

1 Comment

Doesn't necessarily address my question, but nonetheless a measure of good coding.+1

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.