Using a session value to store last seen DateTime

    //----- CHECK FOR USER RETURNING AFTER A WHILE -----
    $LastSeenDateTime = False;
    if (isset($_SESSION['LastSeenDateTime']))
      $LastSeenDateTime = strtotime($_SESSION['LastSeenDateTime']);

    $TheDateTimeNow = strtotime(date("Y-m-d H:i:s"));
  
    if ($LastSeenDateTime === False)
      $LastSeenSecondsAgo = -1;
    else
      $LastSeenSecondsAgo = $TheDateTimeNow - $LastSeenDateTime;
    
    if (
      ($LastSeenSecondsAgo < 0) ||
      ($LastSeenSecondsAgo > (5 * 60 * 60))   //<<<<Set time limit here
    )
    {
      //----------------------------------------------
      //----- USER HAS NOT BEEN SEEN FOR A WHILE -----
      //----------------------------------------------
      
      
    }
        
    //----- STORE NEW LAST SEEN TIMESTAMP -----
    $_SESSION['LastSeenDateTime'] = date("Y-m-d H:i:s");


  //If you want to reset if a user logs out, add this to your logout handler:
  //unset($_SESSION['LastSeenDateTime']);
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.