Skip to content
Advertisement

PHP session cookie not extending timeout on activity

Scenario

  • User visits at 2:00:00PM and a session is assigned that will expire in 30 minutes.
  • User continues activity with requests to the server for 30+ minutes.
  • User’s session expires at 2:30:00PM (30 minutes later) even though they have been active with requests to the server the entire time.

How do I make the session cookie always update the expiration date for both the client and the server so the expiration is extended (in this case, 30 minutes) each time the client makes a request to the server?

<?php
session_name('session');
session_start();
?>

Advertisement

Answer

Adding setcookie after session_start() is working as expected.

<?php
session_name('session');
session_start();
setcookie(session_name(), session_id(), time() + 3600, '/');
?>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement