Back

Implement phpredis correctly as a session driver. | Question


How to implement phpredis correctly as a session driver in PHP.ini and work well in laravel in applying redis as a driver for the session.

This is my php.ini config:

session.save_handler = redis
session.save_path = "tcp://{domain}.com:6379"

and in my config for database, session, cache like this:

Database configuration: app/config/database.php

    'redis' => [

        'client' => 'phpredis',

        'default' => [
            'host' => env('REDIS_HOST', '{doamin}'),
            'password' => env('REDIS_PASSWORD', '{secret}'),
            'port' => env('REDIS_PORT', 6379),
            'scheme' =>  env('REDIS_SCHEME', 'tcp'),
            'database' => env('REDIS_DB', 0),
            'persistent' => env('REDIS_PERSISTENT', true),
            'async' => env('REDIS_ASYNC', true),
            'prefix' => env('REDIS_PREFIX', 'default_'),
            'read_timeout' => env('REDIS_READ_TIMEOUT', 0),
            'read_write_timeout' => env('REDIS_READ_WRITE_TIMEOUT', 0),
            'timeout' => env('REDIS_TIMEOUT', 120),
            'weight' => env('REDIS_WEIGHT', 1)
        ],

        'cache' => [
            'host' => env('REDIS_HOST', '{domain}'),
            'password' => env('REDIS_PASSWORD', '{secret}'),
            'port' => env('REDIS_PORT', 6379),
            'scheme' =>  env('REDIS_SCHEME', 'tcp'),
            'database' => env('REDIS_DB', 1),
            'persistent' => env('REDIS_PERSISTENT', true),
            'async' => env('REDIS_ASYNC', true),
            'prefix' => env('REDIS_PREFIX', 'cache_'),
            'read_timeout' => env('REDIS_READ_TIMEOUT', 20),
            'read_write_timeout' => env('REDIS_READ_WRITE_TIMEOUT', 20),
            'timeout' => env('REDIS_TIMEOUT', 120),
            'weight' => env('REDIS_WEIGHT', 2)
        ],

        'session' => [
            'host' => env('REDIS_HOST', '{domain}'),
            'password' => env('REDIS_PASSWORD', '{secret}'),
            'port' => env('REDIS_PORT', 6379),
            'scheme' =>  env('REDIS_SCHEME', 'tcp'),
            'database' => env('REDIS_DB', 2),
            'persistent' => env('REDIS_PERSISTENT', true),
            'async' => env('REDIS_ASYNC', true),
            'prefix' => env('REDIS_PREFIX', 'session_'),
            'read_timeout' => env('REDIS_READ_TIMEOUT', 20),
            'read_write_timeout' => env('REDIS_READ_WRITE_TIMEOUT', 20),
            'timeout' => env('REDIS_TIMEOUT', 120),
            'weight' => env('REDIS_WEIGHT', 3)
        ],

    ],

Cache configuration: app/config/cache.php

'default' => env('CACHE_DRIVER', 'redis'),
'prefix' => env(
     'CACHE_PREFIX',
     str_slug(env('APP_NAME', '{domain}'), '_').'_cache'
 ),

and session configuration like this: app/config/session.php

'driver' => env('SESSION_DRIVER', 'redis'),
'lifetime' => env('SESSION_LIFETIME', 120),
'connection' => env('SESSION_CONNECTION', 'session'),
'cookie' => env(
     'SESSION_COOKIE',
     str_slug(env('APP_NAME', '{domain}'), '_').'_session'
),
'path' => 'tcp://{domain}:6379?weight=3&prefix'.env('REDIS_PREFIX', 'session_'),
'domain' => env('SESSION_DOMAIN', '.{domain}'),
'same_site' => 'lax',

I also customize my route for the user authenticate. I do not use web route file as my route for the user authenticate, but I create one like this:

profile.php

Auth::route(['verify', true]);
Route::domain(strtolower('account'.config('session.domain')))->middleware(['auth', 'verified'])->group(function(){
   Route::get('/profile', 'ProfileController...');
   Route::get('/setting', 'SettingController...');
})

~ I also use workbox for my service worker.

The problem is while I try to signout from my site then I sign in again, I always get 419 respond. Could you please to guide me to correct my config ? I do not speak english well. Thanks


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us