使用ajax在浏览器中单击时销毁php会话

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

What I have tried so far is ,destroying 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 thing I mentioned and if yes then where I should my ajax code ,in booking.php or index.php ! Thanks in advance

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

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 is.. if the index page is coming from booking page, you can detect it and do whatever you want after that.