Home > OS >  PHP Session works fine under one directory, but the same code fails in a different directory
PHP Session works fine under one directory, but the same code fails in a different directory

Time:08-29

I have the below code that works fine under /t/cgi-bin/test1.php, but the same code fails under /p/cgi-bin/test1.php. Every time I run under /p/cgi-bin..., it takes me to test_log1.html right away. It is not waiting for 15 minutes before it times out to test_log1.html. Any help is appreciated.

<?php
      session_start();
      if(isset($_SESSION["test"]))
      {
                if((time() - $_SESSION['last_login_timestamp']) > 900)
           {
                header("location:../test_log1.html");
           }
           else
           {
                $_SESSION['last_login_timestamp'] = time();
           }
      }
      else
      {
           header('location:../test_log2.html');
    }
?>

CodePudding user response:

Perhaps because by default there is only one session per browser. It's not per tab, or per url.

So, once you have run, the session already has the last_login_timestamp. It then uses the same value for the second session.

  1. If you want them to be different then you have to use different session ids for the two paths.

  2. Another way is to clear/reset the session at the start of the file.

  3. Yet another is to use a different test variable such as test1 and test2.

CodePudding user response:

try: header('Location: '../test_log2.html');

  • Related