Jump to content

How to extend Cookie expiry from user's last activity ?


Recommended Posts

Hi friends,

My Post title says it all. How to extend cookie expiry date from last activity which user has done ?

 

Lets take an example, by default, cookie is set for 30minutes after user login.

userA do login at 1PM, hence given cookie will expire at 1.30PM.

 

Problem with this scenario is that if userA is doing something very important activity in app, then when he click on submit form on any internal link, he will get redirected to login.

Hence he loses his work which he as done.

 

What I want to have is when userA log in at 1PM (First activity), then at first, cookie will expire at 1.30PM.

After that userA become idle, that means he does not click anywhere, he just leave his computer and come back again after 15 minutes i.e. at 1.15PM and starts using php portal, then cookie expiry should become 1.45PM

 

How to do that ? I found script PHPmyadmin has done same thing. It leads to expiration of cookie when user become inactive for more that 1440 seconds.

 

How to do that ?

 

Thanks

Is this a session cookie? If so, you can set the expire date with session_set_cookie_params. If not, then you can simply extend the lifetime of the cookie by calling setcookie with the exact same name and setting a new expire date.

 

You can also use setcookie to extend the session cookie's lifetime.

 

setcookie(
  session_id(),
  serialize($_SESSION),
  time() + 1440/*ini_get('session.cookie_lifetime'),*/,
  ini_get('session.cookie_path'),
  ini_get('session.cookie_domain'),
  ini_get('session.cookie_secure'),
  ini_get('session.cookie_httponly')
);

Edited by ignace

Another option, if there are only a few tasks that can take a lot of time, is to have a AJAX process intermittently send a request to the server (when doing said tasks). A HEAD(er) request will do, as that'll get a new cookie from the server set to expire in another 30 minutes.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.