Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clear session variable after refreshing PHPpage

Tags:

php

I have a page where i have set countdown timer. After the login of user, the count down starts ticking, but when i refresh the page countdown doesn't restart instead it continues with the previous time. i want this timer to restart every time i refresh the page.

am trying this

  <?php
    session_start();
     if(!isset($_SESSION['starttime'])){
     unset($_SESSION['starttime']);
     }
      else if (!isset($_SESSION['starttime']) && isset($_SESSION['start']))       
          {

    $_SESSION['starttime'] = time();
       } 
     ?>

this is timer..

    <?php
    if ( isset($_SESSION['starttime'])) {
    $elapsed = (time() - $_SESSION['starttime']);
   // $elasped = (time () - $starttime);
    if (($elapsed / 60) < 2) {
        echo "var hour=00;";
        if ($elapsed == 0)
            $elapsed = 1;
        echo "min=" . (int) (5 - ($elapsed / 60)) . ";";
        if ($elapsed > 60)
            echo "sec=" . (60 - ($elapsed - 60)) . ";";
        else
            echo "sec=" . (60 - $elapsed) . ";";
    }
    else {
        echo "var hour=0;var min=0;sec=01;";
        unset($_SESSION['starttime'])
        header('location:index.php');
                exit();
    }
}
?>

am stuck here.

like image 716
Sagar Pawar Avatar asked Jun 27 '26 22:06

Sagar Pawar


2 Answers

The isset() function return false if testing variable contains a NULL value.

Change in if condition.

     <?php
     session_start();
     if(isset($_SESSION['starttime'])){ // Change here.
        unset($_SESSION['starttime']);
     }
     else if (!isset($_SESSION['starttime']) && isset($_SESSION['start']))       
     {
        $_SESSION['starttime'] = time();
     }?>
like image 79
Nana Partykar Avatar answered Jun 30 '26 13:06

Nana Partykar


Remove the ! before isset($_SESSION['starttime']). isset() documentation

<?php
    session_start();

    if(isset($_SESSION['starttime'])){ // Change Here
        unset($_SESSION['starttime']);
like image 37
Thaillie Avatar answered Jun 30 '26 11:06

Thaillie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!