Views: 149
Last Modified: 11.05.2022

Kernel supports four variants (files, redis, database, memcache) for session data storage. Storage method is described in bitrix/.settings.php inside the 'session' section:

Files

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'mode' => 'default',
            'handlers' => [
                'general' => [
                    'type' => 'file',       
                ]           
            ],

        ]                   
    ] 
];

Setting for divided session:

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'lifetime' => 14400, // +
            'mode' => 'separated',  // +
            'handlers' => [
                'kernel' => 'encrypted_cookies',  // +           
                'general' => [
                    'type' => 'file',       
                ],
            ],

        ]                   
    ] 
];

Redis

// bitrix/.settings.php
return [
//...        
   'session' => [
       'value' => [
           'mode' => 'default',
           'handlers' => [
               'general' => [
                   'type' => 'redis',  
                   'servers' => [
                       [
                           'port' => 6379,
                           'host' => '127.0.0.1',
                       ],
                       [
                           'port' => 6379,
                           'host' => '127.0.0.2',
                       ],
                       [
                           'port' => 6379,
                           'host' => '127.0.0.3',
                       ],
                   ],
                   'serializer' => \Redis::SERIALIZER_IGBINARY,
                   'persistent' => false,
                   'failover' => \RedisCluster::FAILOVER_DISTRIBUTE,
                   'timeout' => null,
                   'read_timeout' => null,    
               ],          
           ],
       ]                  
   ]
];

Cluster storage of session data

Difference from standard configuration: servers have additional options: serializer, persistent, failover, timeout, read_timeout. You can find detailed information about then in the official documentation.

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'mode' => 'default',
            'handlers' => [
                'general' => [
                    'type' => 'redis',   
                    'servers' => [
                        [
                            'port' => 6379,
                            'host' => '127.0.0.1',
                        ],
                        [
                            'port' => 6379,
                            'host' => '127.0.0.2',
                        ],
                        [
                            'port' => 6379,
                            'host' => '127.0.0.3',
                        ],
                        'serializer' => \Redis::SERIALIZER_IGBINARY,
                        'persistent' => false,
                        'failover' => \RedisCluster::FAILOVER_DISTRIBUTE,
                        'timeout' => null,
                        'read_timeout' => null,
                    ],
                ],           
            ],
        ]                   
    ] 
];

Setting for separate session:

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'lifetime' => 14400, // +
            'mode' => 'separated', // +
            'handlers' => [
                'kernel' => 'encrypted_cookies',  // +  
                'general' => [
                    'type' => 'redis',   
    		    'port' => '6379',
    		    'host' => '127.0.0.1',
                ],           
            ],

        ]                   
    ] 
];

Memcache

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'mode' => 'default',
            'handlers' => [
                'general' => [
                    'type' => 'memcache',   
    		    'port' => '11211',
    		    'host' => '127.0.0.1',
                ],           
            ],

        ]                   
    ] 
];

Cluster storage for session data

When you need to create a cluster from server memcache, you just need to add the servers settings.

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'mode' => 'default',
            'handlers' => [
                'general' => [
                    'type' => 'memcache',   
                    'servers' => [
                        [
                            'port' => 11211,
                            'host' => '127.0.0.1',
                            'weight' => 1, //read more about the weight settings in the memcahe-related documentation
                        ],
                        [
                            'port' => 11211,
                            'host' => '127.0.0.2',
                        ],
                    ],
                ],           
            ],
        
        ]                   
    ] 
];

Setting for separated session:

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'lifetime' => 14400, // +
            'mode' => 'separated', // +
            'handlers' => [
                'kernel' => 'encrypted_cookies',  // +  
                'general' => [
                    'type' => 'memcache',   
    		    'port' => '11211',
    		    'host' => '127.0.0.1',
                ],           
            ],

        ]                   
    ] 
];

Mysql

Data is stored in the table b_user_session

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'mode' => 'default',
            'handlers' => [
                'general' => [
                    'type' => 'database',       
                ]           
            ],         
        ]                   
    ] 
];

Setting for separate session:

// bitrix/.settings.php
return [
//...        
    'session' => [
        'value' => [
            'lifetime' => 14400, // +
            'mode' => 'separated', // +
            'handlers' => [
                'kernel' => 'encrypted_cookies',  // +  
                'general' => [
                    'type' => 'database',       
                ]           
            ],         
        ]                   
    ] 
];



Courses developed by Bitrix24