-
Notifications
You must be signed in to change notification settings - Fork 0
FAQ
No. It's a convenience layer over native PHP sessions. Session::set('x', 1)
and $_SESSION['x'] = 1 are equivalent, and you can mix both freely. The value of
the facade is the fluent API, predictable exceptions, and pluggable
adapters.
It bootstraps the facade once and hands you an instance to chain from. Think of it as "set up the session machinery for this request." Call it a single time, before any other method.
Both work and operate on the same session. The wiki uses the static style
(Session::set(...)) because it reads well and matches PHP's own
session_*() mental model. Use instance calls if you prefer dependency-style
passing — see The Session Facade.
No — a PHP session has exactly one save handler. Pick one adapter per request and
pass it to createImmutable(). You can choose the adapter dynamically (e.g.
from config) at bootstrap; see the Recipes.
It depends on the adapter:
- Redis / Memcache(d) — per-key/item TTL; the backend evicts them.
-
File / PDO / MongoDB — via
gc(), on PHP's probability-based GC schedule (or a cron you run yourself). - Cookie — the cookie's own lifetime, enforced by the browser.
Remember the session cookie lifetime (session.cookie_lifetime) is separate
from the storage TTL — align them to avoid "logged out but data still there" (or
vice versa) surprises.
With a server-side adapter you can clear the store directly. For
PDO: DELETE FROM sessions;.
For Redis: delete the keys under your prefix. Per-user global logout needs a
lookup you maintain (store session ids by user id at login).
The payload is encrypted (AES-256-CTR + SHA-256) via initphp/encryption, so
clients can't read or tamper with it; a bad cookie is treated as an empty
session. But it lives in the browser, so keep it small (cookies are ~4 KB) and
keep the key secret and stable across servers. See Cookie Adapter.
flush() and unset() require an active session. Call start() first, or guard
with isStarted(). See Session Lifecycle.
The facade only routes its documented methods. Check the spelling against the
API Reference; a typo (or a method that doesn't exist) raises
SessionException.
That's by design — construction-time failures are loud:
- missing extension/library →
SessionNotSupportedAdapter - missing/invalid option →
SessionInvalidArgumentException - unreachable backend →
SessionAdapterException
See Exceptions.
PHP's session machinery calls save handlers and expects boolean/string results,
not exceptions. So adapters return false (or '' from read() for an absent
session) at runtime, and reserve throwing for construction time.
- Pure
$_SESSIONlogic (theGetterSettermethods) can be tested by setting$_SESSIONdirectly — no real session needed. - Tests that call
start()/destroy()should run in an isolated process (PHPUnit's#[RunInSeparateProcess]+#[PreserveGlobalState(false)]) and pass['use_cookies' => '0']to avoid header output. - For adapters, the File adapter (temp dir) and
PDO adapter (SQLite
:memory:) test cleanly without external services.
No — those are caching standards. This package targets PHP's session model. The
adapters implement PHP's native SessionHandlerInterface (via
AdapterInterface), so they plug
straight into session_set_save_handler().
Yes — extend AbstractAdapter and implement read/write/destroy. See
Custom Adapters.
- Bugs: GitHub issues
- Questions/ideas: InitPHP discussions
- Security: see the Security Policy
initphp/sessions · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Usage
Adapters
- Adapters Overview
- File Adapter
- Redis Adapter
- PDO Adapter
- Cookie Adapter
- Memcache / Memcached Adapter
- MongoDB Adapter
- Custom Adapters
Reference
Practical Guides
Migration & Help