Magearlik
Magearlik

Reputation: 523

How to print session time remaining in php?

I have a program in PHP and I want to print to the screen how much time the user has until their session expires. I want it to update when they reload the page.

For example a simple message like this:

Your session expires in 60 minutes

...page reload...

Your session expires in 59 minutes

Also, how do I set the timeout value?

Upvotes: 2

Views: 7511

Answers (1)

Andreas
Andreas

Reputation: 23958

You can set a cookie with the UNIX time and compare with that.

setcookie("start", time(), time ()+3600); // saves a cookie with current UNIX time for one hour

Read about setcookie here http://php.net/manual/en/function.setcookie.php
If you never used it before, be sure to read the begining carefully.

And compare time like:

If (isset($_COOKIE["start"]) and $_COOKIE["start"]+3600<time()){
    echo (time()-$_COOKIE["start"])/60 . " minutes left";
}Else{
    Echo "times up";
}

Upvotes: 2

Related Questions