Reputation: 121
I am building a site in Laravel 5, I need to manage session as something like this: For Example. I have a site [email protected] hosting on server X and another [email protected] hosting on server Y along with database,(both server are of different countries) I need to use same database for the both site but session management is typical task for me in [email protected] as database is hosting on [email protected]. I am using Auth in laravel for authentication How that will be possible please help-.
Upvotes: 0
Views: 2627
Reputation: 1916
You should use the same database connection on both servers. You can either make a small third server just for session management or you can simply tell X server to connect to the Y server database. You will first start by setting the environment variable SESSION_DRIVER
or the configuration property session.driver
to: database
or redis
depending on what you are using. Then create a connection the config file database.php
under connections
property if it's a RDBMS or under redis
if it's a redis database.
'connections' => [
// ...
'session' => [
'driver' => 'mysql',
'host' => env('SESSION_DB_HOST'),
'database' => env('SESSION_DB_NAME'),
'username' => env('SESSION_DB_USERNAME'),
'password' => env('SESSION_DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]
// Or
'redis' => [
// ...
'session' => [
'host' => env('SESSION_REDIS_HOST'),
'port' => env('SESSION_REDIS_PORT', 6379),
'database' => 0,
],
]
Then in the file session.php
, change the the value of connection
to the name of the connection you want, session
in this case.
Be mindful that you need to open the required ports and do some authorization process and take security measures on the host server (Y server since it has the database).
---- Answering the comment
Session driver is the mechanism that laravel uses to manage sessions. It can be on file, database, redis... So, when you choose the session driver in its config file and the driver is a database, you would specify a database connection for it or it will use the default database connection. So, if you create a third server and you want to use Redis as the database, you would open port 6379 (in case you're using the default redis configuration), then you create a connection in your Laravel installations. The connection in database.php
will be like so:
'redis' => [
// ...
'my_session' => [
'host' => env('SESSION_REDIS_HOST'),
'port' => env('SESSION_REDIS_PORT', 6379),
'database' => 0,
],
]
Then in your environment or in the .env
you add:
SESSION_REDIS_HOST=xx.xx.xx.xx // the ip address or domain of the third server
SESSION_REDIS_PORT= 6379
SESSION_DRIVER=redis
And finally, in you session.php
config, you would set these values:
//...
'connection' => 'my_session',
You would do this on all Laravel installations that you want to be connected to the same session database.
Upvotes: 2
Reputation: 731
Laravel provides database sessions which stores sessions in database so that you can use multiple servers for your application. Have a look at https://laravel.com/docs/5.3/session#database-sessions
Upvotes: 0