I am looking to destroy the session when a user clicks "Log Out", I am trying to do this by calling a function onClick using jQuery which will then call a .PHP file which contains, session_destroy();.
index.php
<?php
if(isset($_SESSION['username'])) {
echo "<li><a href='#' id='logOut' onclick='logOut();'>Log Out</a></li>";
}
?>
This makes the log out button visible only when a user is logged in and when onClick should call logOut() which is contained in Nav.js.
Nav.js
function logOut() {
$.get("../Controller/logOut.php");
return false;
}
logOut.php
<?php
session_destroy();
?>
When I click "Log Out" nothing seems to happen and I am not getting any errors in Chrome Dev console. What is the problem?
You must have to start session before calling session_destroy()
<?php
session_start();
session_destroy();
header("Location: login.php"); //redirect your page to login page
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With