shalamhez shalamhez - 4 months ago 14
PHP Question

php session every 24 hours to be cleaned

I want one session variable to be cleaned every 24 hours .I don't want to kill or unset all the sessions just one session to be unset every 24 hours a day.

Answer

When the session is 1st created on the client just give it a date:

if(!isset($_SESSION['date'])
    $_SESSION['date'] = date('m_d_y');

Then whenever the page changes check that date:

if($_SESSION['date'] == date('m_d_y')){
   //still today
}
else {
   //destroy session
}

Or you could do it with timestamp and check based on the number of hours:

if(!isset($_SESSION['creationTime'])
    $_SESSION['creationTime'] = time();

if (time() - $_SESSION['creationTime'] <= 60*60*24 ){
   //still today
}
else {
   //destroy session
}
Comments