Reputation: 101
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!
Upvotes: 0
Views: 2075
Reputation: 3437
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.
Upvotes: 1
Reputation: 471
The session is being unset by the ajax call but you will need to manipulate the divs accordingly after success logout ajax call like hide the username place a login button or remove any user area....
Upvotes: 0
Reputation: 11984
<?php
session_start();
if(isset($_POST['account_type']) && $_POST['account_type'] == "twitter") {
session_destroy();
header("Location: index.php");
}
?>
Upvotes: 0