John Davidson

php - Laravel - Same Session Over Multiples Projects - Using Same Database and Domain

0 comments
Message:


I have two laravel 7 projects, which communicate in the same database.


serv1 : C:/xampp/htdocs/serv1/public


serv2:
C:/xampp/htdocs/serv2/public


I would like that , for example , when authenticating in which one of these applications , he would authenticate in both .



My Answer:
To achieve this, you can use the same session driver and domain for both Laravel projects. Here's how you can set it up:

1. Set the same session driver for both projects in the `.env` file. You can use the database session driver for this purpose. Make sure both projects are using the same database connection.


SESSION_DRIVER=database


2. Set the same session domain for both projects in the `config/session.php` file. Update the `domain` key to be the same for both projects.

php
'domain' => '.yourdomain.com',


3. Update the session configuration in both projects to use the same database connection. In the `config/database.php` file, update the `connections` array to use the same database connection for the session table.

php
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],


4. Migrate the session table to the database for both projects using the following command:


php artisan session:table
php artisan migrate


With these configurations in place, both Laravel projects should now share the same session data and authenticate users across both projects. When a user logs in to one project, they should also be authenticated in the other project.

Rate this post

3 of 5 based on 2523 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us