0

What I want is to be able to delete session data without page refresh. So let's say my user created a session and left his computer on sleep mode for two years, I want to delete his session after 4800s... How can I do that?

At the moment, I have a code which only deletеs session on refresh only.

if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 4800)) {
    session_unset();     // unset $_SESSION variable for the run-time 
    session_destroy();  // destroy session data in storage
}

$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp  

Or does this code work without page refresh? If not, how can constantly update this code?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • If they make another request to the server, Then your code will run and eventually destroy the session. – Akar Feb 25 '15 at 18:11
  • I think JavaScript is a better fit for this, as it operates on the client side, not the server side. – Claies Feb 25 '15 at 18:12
  • The code won't update itself unless you tell it to. Add a listener to it that refreshes every time interval you need. – Funk Forty Niner Feb 25 '15 at 18:13
  • 1
    *"So lets say my user created a session and left his computer on sleep mode for two years"* - There's bound to be a power failure and that person's going to have to eventually recharge their batteries. Who leaves a computer on for 2 years? – Funk Forty Niner Feb 25 '15 at 18:20
  • Thanks a lot guys, I will go with javascript 'setTimeout' function... I knew that I can use it, but just wanted to make sure there's nothing else out there..... – hello_everyone Feb 25 '15 at 19:40

2 Answers2

1

Just add this code anywhere in your page

<script type="text/javascript">
  setTimeout(function(){
    location = ''
  },60000)
</script>

reference: Refresh Page for interval using js

Dharman
  • 30,962
  • 25
  • 85
  • 135
Atif
  • 280
  • 1
  • 11
0

100% Working Just Add This Code Once

setTimeout(function () {
window.location.href = "";
}, 3000);
Paresh Shiyal
  • 534
  • 4
  • 15