php session every 24 hours to be cleaned
I want one session variable to be cleaned every开发者_如何学C 24 hours .I don't want to kill or unset all the sessions just one session to be unset every 24 hours a day.
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
}
try to set Cookie expired time into now() + 24 hours...
When you creating session, write to it timestamp. Then you are using seesion check the actually timestamp and of creating session, then if difference is grather than 86400, then drop session and create new one.
This solution prevent users from using sessions older than 24hours. You can apply other comparing algorithm to eg. prevent users from using session before 1AM of current day. Then will be work exaclly same when you will want erase session every 24hours in 1AM every day.
When create session (maybe when user login), declare session timeout:
session_start();
$_SESSION["timeout"] = time()+ (60*60*24);
Create backend function / page where jquery can call every 10 seconds / 5 seconds up to you (I save it as get_session.php):
session_start();
$session_life = time() - $_SESSION["timeout"];
$inactive = 0;
if($session_life > $inactive){
session_destroy();
echo 'Destroyed';
}
The jquery script run every 5 second (recommended at master page / template header / footer of the page):
<script type="text/javascript">
window.setInterval(function(){
sessionHeartBeat();
}, 5000);
function sessionHeartBeat(){
$.ajax({
url: 'get_session.php',
success: function(response){
console.log(response);
}
});
}
</script>
精彩评论