2

I run multiple websites from a single laravel app. Is it possible to have different session lifetimes for them?

E.g. one site should have the standard 2 hours and another should be 1 year

Chris
  • 13,100
  • 23
  • 79
  • 162
  • Looking through the source it seems it reads the config directly... So I'd be interested to see what people can come up with on this one – Chris Mar 20 '18 at 11:35
  • Possible duplicate of [Laravel: share session data over multiple domains](https://stackoverflow.com/questions/26821648/laravel-share-session-data-over-multiple-domains) – Adam Kozlowski Mar 20 '18 at 12:52
  • check also: https://laracasts.com/discuss/channels/laravel/share-session-from-multiple-domains-but-on-same-server – Adam Kozlowski Mar 20 '18 at 12:52
  • @AdamKozlowski I do not want to share session between domains. I just want to have different session lifetime values for the different domains – Chris Mar 20 '18 at 13:16

2 Answers2

0

You can set a variable for each domain in the .env file:

SESSION_LIFE_TIME=160

and call this variable in your config file:

'lifetime' => env('SESSION_LIFE_TIME', 120),

Hope this works for you.

Mahmoud Abdelsattar
  • 1,299
  • 1
  • 15
  • 31
  • unfortunately the domain is determined dynamically. i.e. it is not known beforehand and therefore I cannot use env variables. I would need a way that allows me to set it on the fly with an if condition – Chris Mar 20 '18 at 14:47
  • Did you try to create a helper function like `function getSessionPerDomain(){ switch ($domain){ case ('domain1'): return 120; // .. etc. } }` and call it directly in the config ? – Mahmoud Abdelsattar Mar 20 '18 at 14:59
0
  1. you can set default laravel session lifetime

config/session.php

'lifetime' => 120
  1. Create Session_life table in database server and add session lifetime value for particular website.

  2. In controller where your login functionality set lifetime comes from table:

$sl = Session_life ::where('website', "www/test1.com")->first();
$defaultSession = $sl->session_value;
$request->session()->put('lifetime', $defaultSession);
config()->set('session.lifetime', $defaultSession);
config(['session.lifetime' =>  $defaultSession]);

Here, Session_life is table name, session_value is lifetime value store in table.

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32