Skip to content
Greg Bowler edited this page Apr 23, 2026 · 11 revisions

phpgt/session gives us an object-oriented way to work with PHP session data without reading and writing the $_SESSION superglobal directly.

Sessions are useful whenever a web application needs to remember short-lived information about one visitor between HTTP requests. Common examples include whether someone is logged in, which step of a form they are on, or which message should be shown after a redirect.

PHP already has session support, but the native API stores everything in global state. This library wraps the session in objects, so we can pass only the session area a piece of code needs.

Note

In WebEngine applications, the session is created during request initialisation and placed into the service container. Page logic can ask for GT\Session\Session directly, without constructing it manually.

What this library covers

  • A Session object that starts PHP's session mechanism and reads persisted data.
  • Dot-notation keys for grouping session data into named areas.
  • SessionStore objects that can be passed to one part of an application without giving access to the whole session.
  • Type-safe getters through phpgt/typesafegetter.
  • File-backed session storage through FileHandler.
  • Redis-compatible shared session storage through RedisHandler.
  • Flash messages for one-request notifications.

A small example

use GT\Session\Session;

function showAccount(Session $session):void {
	if($session->contains("auth.userId")) {
		echo "Signed in as user ", $session->getInt("auth.userId");
		return;
	}

	echo "Please sign in.";
}

Here the auth.userId key means: read the userId value from the auth area of the session. That same auth area can be passed to another class as its own SessionStore, so that class cannot accidentally read or write unrelated session data.


To see a complete setup from scratch, continue with the Quick start guide.

Clone this wiki locally