views:

155

answers:

1

How would I display the current amount of time left of the cookie/session for a logged in user of WordPress? So for example if the cookie timeout is 24 hours and the user has been logged in for 2 hours then the output would be 22 hours.

+1  A: 

I don't think this is possible because you can't determine a cookie's expiry date from PHP or Javascript. You may be able to build something on top of the existing Wordpress log in system to store the date in another cookie when a user logs in.


Actually, it looks like Wordpress stores the log in expiry date (along with other log in details) in a cookie prefixed 'wordpress_logged_in_'. So, you should be able to do something like the below to display the time until expiry.

foreach ($_COOKIE as $key => $cookie) {
    if (strpos($key, 'wordpress_logged_in_') === 0) {
        $cookie_array = explode('|', $cookie);
        $expiry_time = $cookie_array[1];
        echo human_time_diff(mktime(), $expiry_time);
        break;
    }
}
Richard M