basic_space basic_space - 25 days ago 13
PHP Question

How to unset session without reloading page

I want to log the user out without reloading the page, just updating some divs. This is what I have but obviously not working. The session unsets only after reloading the page.

$.ajax({
url: "logout.php",
method: "post",
data: "account_type=twitter",
success: function () {

/*****do something***********/
}
});


in logout.php

<?php
session_start();

if(isset($_POST['account_type']) && $_POST['account_type'] == "twitter") {

unset($_SESSION['twitterId']);
unset($_SESSION['twitterName']);
unset($_SESSION['twitterPic']);
unset($_SESSION['twitterUrl']);
unset($_SESSION['access_token']);
unset($_SESSION['token_oauth']);

header("Location: index.php");
}
?>


some help would be greatly appreciated. Thanks!

Answer

Technically there is nothing wrong with what you are doing.

The problem is with the expectation that logging out will change content that has already been displayed.

When you are logging the user out via an ajax call, the old logged in content will still be shown on the page until you actually either reload the entire page, or reload the content of the div that is displaying the logged in content.

For instance if you have a box that says You are logged in, just logging out via ajax will not change that content until you reload that div using ajax or reload the entire page so the server then tells the page that you are not logged in.

Content that has already been sent to the user will always remain as it was unless you specifically change it using either ajax after logging out or by reloading all the content on the page.

Comments