Sessions
ZealPHP overrides all session_*() functions via uopz at startup. Your code calls the same PHP functions — they write to the coroutine-local G::instance()->session instead of the global $_SESSION.
How it works under the hood
// At App::__construct() time — runs once per server lifecycle:
\uopz_set_return('session_start', \Closure::fromCallable('ZealPHP\Session\zeal_session_start'));
\uopz_set_return('session_id', \Closure::fromCallable('ZealPHP\Session\zeal_session_id'));
\uopz_set_return('session_write_close', \Closure::fromCallable('ZealPHP\Session\zeal_session_write_close'));
// ... + 15 more functions
// Your code stays unchanged:
session_start();
$_SESSION['user'] = ['id' => 42, 'name' => 'alice'];
session_write_close();
// → writes to G::instance()->session, not $GLOBALS['_SESSION']
Overridden functions
| Native PHP | ZealPHP replacement | Notes |
|---|---|---|
session_start() | zeal_session_start() | Reads session file into G::session |
session_id() | zeal_session_id() | Gets/sets session ID from cookie or G::cookie |
session_write_close() | zeal_session_write_close() | Serializes G::session to file |
session_destroy() | zeal_session_destroy() | Deletes session file |
session_regenerate_id() | zeal_session_regenerate_id() | Renames session file with new ID |
session_unset() | zeal_session_unset() | Clears all session data |
GET
Write session data
$app->route('/demo/session/write', function() {
$g = G::instance();
// session_start() is called automatically by CoSessionManager per request
$g->session['user'] = ['id' => 1, 'name' => 'alice'];
$g->session['login_at']= time();
return ['written' => $g->session['user']];
});
LIVE OUTPUT
Click Run →
GET
Read session data back
$app->route('/demo/session/read', function() {
$g = G::instance();
return [
'session_keys' => array_keys($g->session),
'has_user' => isset($g->session['user']),
'session_id' => session_id(),
];
});
LIVE OUTPUT
Click Run →
Sessions are per-coroutine in coroutine mode. Each request gets its own isolated
G::instance()->session via Coroutine::getContext() —
no data leaks between concurrent requests.