Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run PHP onClick - session_destroy

Tags:

html

jquery

php

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?

like image 935
Colin747 Avatar asked Sep 21 '26 01:09

Colin747


1 Answers

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
?>
like image 192
Sumit Bijvani Avatar answered Sep 23 '26 17:09

Sumit Bijvani