Loading...
Loading...
Covers Bitrix sessions — Application::getSession(), getKernelSession(), getLocalSession(), BX_SECURITY_SESSION_READONLY and BX_SECURITY_SESSION_VIRTUAL modes, storages (cache, database, redis, null session handler), separated session mode in .settings.php. Applied instead of direct $_SESSION access, when optimizing AJAX session locks, configuring alternative storages, and separating kernel/local sessions. Key terms — session, getSession, session storage, BX_SECURITY_SESSION_READONLY, separated session, getKernelSession, getLocalSession.
npx skill4agent add bxmaximum/bitrix-framework-skills bitrix-sessions$_SESSIONreadonlyuse Bitrix\Main\Application;
$session = Application::getInstance()->getSession();
if (!$session->has('cart'))
{
$session->set('cart', ['items' => []]);
}
$session['cart']['items'][] = $productId;
$session['cart'] = $cart; // set via ArrayAccess
$session->remove('flash_message');
$session->clear(); // remove everythingBitrix\Main\Session\SessionInterfaceArrayAccess$kernelSession = Application::getInstance()->getKernelSession();
$kernelSession->set('UF_LAST_LOGIN', time());separated$session->set(...)main 20.5.400session_id()$local = Application::getInstance()->getLocalSession('cart');
if (!isset($local['productIds']))
{
$local->set('productIds', [1, 2, 3]);
$local->set('total', 42);
}
$ids = $local->get('productIds');cache.settings.php$_SESSION$_SESSION// before including prolog
define('BX_SECURITY_SESSION_READONLY', true);
require $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php';flockdefine('BX_SECURITY_SESSION_VIRTUAL', true);.settings.php'session' => [
'value' => [
'mode' => 'separated',
'lifetime' => 14400,
'handlers' => [
'kernel' => 'encrypted_cookies',
'general' => ['type' => 'redis', 'host' => '127.0.0.1', 'port' => 6379],
],
],
],$_SESSION['BX']/local/.settings.php/bitrix/.settings.phpsession.value.handlers.general.type| type | When | Note |
|---|---|---|
| Dev, small projects | Lock by |
| High-load, clusters | Supports |
| Legacy projects | No persistence |
| When no cache servers | |
'session' => [
'value' => [
'mode' => 'default',
'handlers' => [
'general' => [
'type' => 'redis',
'servers' => [
['host' => '10.0.0.1', 'port' => 6379],
['host' => '10.0.0.2', 'port' => 6379],
['host' => '10.0.0.3', 'port' => 6379],
],
'serializer' => \Redis::SERIALIZER_IGBINARY,
'persistent' => false,
'failover' => \RedisCluster::FAILOVER_DISTRIBUTE,
'timeout' => null,
'readTimeout' => null, // camelCase (session Redis handler)
],
],
],
],'handlers' => [
'general' => [
'type' => 'memcache',
'servers' => [
['host' => '10.0.0.1', 'port' => 11211, 'weight' => 1],
['host' => '10.0.0.2', 'port' => 11211],
],
],
],'handlers' => [
'general' => ['type' => 'database'], // b_user_session table
],'session' => [
'value' => [
'lifetime' => 14400, // seconds
'mode' => 'default',
'regenerateIdAfterLogin' => true, // recommended: fixation protection
'ignoreSessionStartErrors' => false, // true — hit continues even if Redis is unavailable
'handlers' => [ ... ],
],
],$session = Application::getInstance()->getSession();
$session->set('flash.success', 'Post saved');
// next request:
if ($msg = $session->get('flash.success'))
{
$session->remove('flash.success');
echo htmlspecialcharsbx($msg);
}$session->regenerateId()regenerateIdAfterLogin = trueHttpOnlySecureSameSite=Lax|Strictsession.cookie_*bitrix-security