NativeSessionDriver wraps PHP's built-in session engine. It is the default driver used by SessionManager.
use CommonPHP\Session\Drivers\NativeSessionDriver;
$driver = new NativeSessionDriver(
options: ['cookie_lifetime' => 3600],
configuredName: 'APPSESSID',
);use CommonPHP\Session\SessionManager;
$session = SessionManager::native(
options: ['cookie_lifetime' => 3600],
name: 'APPSESSID',
);The options array is passed to session_start():
$session = SessionManager::native([
'cookie_lifetime' => 3600,
'cookie_secure' => true,
'cookie_httponly' => true,
'cookie_samesite' => 'Lax',
]);Use regular PHP session configuration for settings that should apply process-wide.
Set the session name or ID before start:
$session->setName('APPSESSID');
$session->setId('known-id');
$session->start();The native driver rejects empty names and IDs. It also rejects name or ID changes while a session is active.
The driver maps session_status() to SessionStatus:
PHP_SESSION_DISABLEDbecomesSessionStatus::Disabled;PHP_SESSION_NONEbecomesSessionStatus::None;PHP_SESSION_ACTIVEbecomesSessionStatus::Active.
data() returns $_SESSION by reference.
$driver->start();
$data = &$driver->data();
$data['user_id'] = 42;If $_SESSION is missing or not an array after start, the native driver initializes it to an empty array.
$driver->save();This calls session_write_close().
$driver->invalidate();This clears $_SESSION, expires the active session cookie when cookie sessions are enabled and headers are still available, then calls session_destroy().
Native session_* functions often report failures as warnings. The driver captures those warnings and throws CommonPHP exceptions such as SessionStartException or SessionStorageException.