Home > Software engineering >  Laravel - How to share the session between two or more Web Application?
Laravel - How to share the session between two or more Web Application?

Time:02-25

I have two web Applications. I will login in to one Web Application and will navigate to another by links or redirection from the first Application. Lastly after completing some steps in Application two, I will be redirected to Application one. How can I implement this?

CodePudding user response:

Technically, session between two web application (two different WARs) cannot be shared. This is done for security reasons.

But you can do something like this when you are calling app2 from app1, pass all necessary information via the request object (as a request params) then app2 can read and create the session there (perhaps a servlet/filter can be used for this).

You can also look at this.

CodePudding user response:

Create a new database called sessions.

Configure a connection profile for the session database secondary to your apps primary databases for both apps.

And then they should be syncing up in storing the data for sessions, being able to share them etc...

config/database.php

'app_main_database' => [
    'driver'    => env('DB_CONNECTION'),
    'host'      => env('DB_HOST'),
    'port'      => env('DB_PORT'),
    'database'  => env('DB_DATABASE'),
    'username'  => env('DB_USERNAME'),
    'password'  => env('DB_PASSWORD'),
],

'sessions_database' => [
    'driver'    => env('DB_CONNECTION_SESSION'),
    'host'      => env('DB_HOST_SESSION'),
    'port'      => env('DB_PORT_SESSION'),
    'database'  => env('DB_DATABASE_SESSION'),
    'username'  => env('DB_USERNAME_SESSION'),
    'password'  => env('DB_PASSWORD_SESSION'),
],

Configure session.connection to the name for your session driver

config/session.php

<?php

use Illuminate\Support\Str;

return [

    'driver' => env('SESSION_DRIVER', 'database'),

    'connection' => env('SESSION_CONNECTION', 'sessions_database'),
  • Related