PHP Session和JavaScript window.location.replace

My use of:

window.location.replace("calendar.php");

Seems to be not working at all with my PHP session variables. When I use this call I can not use the back button with my code:

<?php
  session_start();

  if($_SESSION['loggedin'] != TRUE) {
    header("Location: index.php");
  }
?>

Does anyone know why this may be? Thanks.

I think you want window.location.href = "calendar.php"; instead. That should hang on to your session variables, as well as not breaking the back button.

This is why you do have :

  • window.location.replace("calendar.php");

but also

-window.location.assign("calendar.php");

the first one might clear the session while the second will keep it.