Skip to content
Greg Bowler edited this page Mar 14, 2023 · 4 revisions

A session is a mechanism to temporarily store data for a particular user of an application. It's particularly useful to store the authentication state for the current user, or other transient information like the user's currently selected timezone, currency, language, etc.

A user of your application is assigned a unique ID, called the session ID. This ID is stored within a cookie on the user's browser, and is subsequently sent along with every request the user's browser makes. With this information, PHP can persist data for each user.

All session data should be seen as temporary. When the user's browser closes, the session ID is wiped - it's up to you to persist the data in some way permanent, such as to a database or filesystem.

An object oriented session interface

Native PHP exposes all data stored to the session in the $_SESSION superglobal, which is readable and writable by any line of code in your project, or any line of code in any third party dependencies you rely on. For this reason, this repository enforces a major benefit of object oriented coding: encapsulation.

Encapsulation means that instead of all code having full access to the session data, it's your job as the developer to decide what areas of code have access to the data stored in the session. This repository also introduces a way to namespace session data, so certain areas of code can only read/write one subset of the session data.

An example of what using PHP.Gt/Session looks like in your PHP:

function example(Session $session):void {
// Access session data using dot notation:
	if($session->get("auth.user")) {
	else {
// Pass other areas of your code just one namespace:
		login($session->getStore("auth", true));

To get started, read the overview.