Destroying php session on click in browser with ajax

I have two files index.php (first) and second booking.php (second) that I want when someone moves from .php to index.php the session gets destroyed.

What I have tried so far is destroying the session with ajax

Here is my relevant code in booking.php file

$(window).on("popstate", function (event, state) {

    $.ajax({
          type: 'GET',
          url: 'logout.php',
          async:yes,
          success: function(msg) {
              if (msg == 'loggedOut') {
            window.location.href = 'index.php';
              }
          }
      });
    });

      

And here is my logout.php file

<?php
session_start();
session_destroy();
echo "loggedOut";
?>

      

Is there any possible way to do above what I mentioned and if so where do I need my ajax code, in booking.php or index.php! thanks in advance

+3


source to share


2 answers


you have to reset the session id in your php file.



session_start(); // fetch OR re-start current session

session_regenerate_id(true); // Update the current session id with a newly generated one

$_SESSION=array(); // empty session data

session_write_close(); // Write session data and end session

      

0


source


In your booking.php file add this

$_SESSION["booking_page"] =true;

      

In your index.php



if(isset($_SESSION["booking_page"]) &&  $_SESSION["booking_page"]===true){
$_SESSION["booking_page"]=false;
//your logout code here; 
}

      

So what happens if .. the index page comes from the booking page, you can find it and do whatever you want after that.

0


source







All Articles