Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Support persistent sessions #28

Conversation

weierophinney
Copy link
Member

PHP supports persistent sessions by allowing developers to set the session.cookie_lifetime value, or to pass the $lifetime argument to session_set_cookie_params(). In each case, ext-session will then set an Expires directive in the Set-Cookie header associated with the session. These values are often manipulated at runtime to allow developers to set session cookie lifetimes based on specific criteria (e.g., a user checking a "remember me" box).

This patch adds the ability for developers to provide at runtime a TTL for the sesssion they are manipulating. It introduces a new interface, SessionCookiePersistenceInterface, with the methods persistSessionFor(int $duration) : void and getSessionLifetime() : int. The first can be used by developers to indicate the desired session lifetime; the second can be used by persistence engines in order to set the lifetime either in the persistence engine itself or in client-side artifacts such as session cookies.

In order to allow the lifetime to persist when a cookie is regenerated, I both recommend that the session stores the lifetime within its own data, and that Session instances use that value when present. I have implemented Session such that it does exactly this, using the value of SessionCookiePersistenceInterface::SESSION_LIFETIME_KEY as the session data key under which the lifetime is stored.

The value is specified and stored as an integer, as most existing systems expect an integer indicating the number of seconds the session should persist. Negative values and zero indicate expiry as soon as the current session is over (generally indicated by closing the window and/or browser).

PHP supports persistent sessions by allowing developers to set the
`session.cookie_lifetime` value, or to pass the `$lifetime` argument to
`session_set_cookie_params()`. In each case, ext-session will then set
an `Expires` directive in the `Set-Cookie` header associated with the
session. These values are often manipulated at runtime to allow
developers to set session cookie lifetimes based on specific criteria
(e.g., a user checking a "remember me" box).

This patch adds the ability for developers to provide at runtime a TTL
for the sesssion they are manipulating. It introduces a new interface,
`SessionCookiePersistenceInterface`, with the methods
`persistSessionFor(int $duration) : void` and `getSessionLifetime() :
int`. The first can be used by developers to indicate the desired
session lifetime; the second can be used by persistence engines in order
to set the lifetime either in the persistence engine itself or in
client-side artifacts such as session cookies.

In order to allow the lifetime to persist when a cookie is regenerated,
I both recommend that the session stores the lifetime within its own
data, and that `Session` instances use that value when present. I have
implemented `Session` such that it does exactly this, using the value of
`SessionCookiePersistenceInterface::SESSION_LIFETIME_KEY` as the session
data key under which the lifetime is stored.

The value is specified and stored as an integer, as most existing
systems expect an integer indicating the number of seconds the session
should persist. Negative values and zero indicate expiry as soon as the
current session is over (generally indicated by closing the window
and/or browser).
Makes a few changes in the newly added documentation to make it more
clear to users.
@weierophinney weierophinney merged commit 8847a6d into zendframework:develop Oct 30, 2018
@weierophinney weierophinney deleted the feature/session-cookie-persistence branch October 30, 2018 21:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants