3

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

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Possible duplicate of [PHP - Session\_Destroy upon pressing Back button](https://stackoverflow.com/questions/14529975/php-session-destroy-upon-pressing-back-button) – PL200 Jul 30 '17 at 09:42
  • No I need ajax to do so ,its not duplicate,I have already tried with php ,but php doesnt have any function to catch event of pressing browser back button –  Jul 30 '17 at 09:44

2 Answers2

0

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
vjy tiwari
  • 843
  • 1
  • 5
  • 17
0

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.

lijnam
  • 85
  • 10
  • @gANDALF Well, Session is server side and ajax is client side. you can't work with session with ajax only. you must send request to the server to destroy or append sessions – lijnam Jul 30 '17 at 10:45
  • thats what I am doing above ,sending an ajax request ,what I want is when I press browser's back button a function is called and ajax sends request on server to destroy sessions –  Jul 30 '17 at 10:47
  • @gANDALF what you need is to detect if the user have pressed back button and then run your ajax code instead of the default even have a look at this https://stackoverflow.com/questions/17594413/javascript-or-jquery-browser-back-button-click-detector – lijnam Jul 30 '17 at 11:03