Search code examples
phpsessionsession-cookies

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();
?>

Solution

  • Adding setcookie after session_start() is working as expected.

    <?php
    session_name('session');
    session_start();
    setcookie(session_name(), session_id(), time() + 3600, '/');
    ?>