kodedphp /
session
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Koded package. |
||
| 5 | * |
||
| 6 | * (c) Mihail Binev <[email protected]> |
||
| 7 | * |
||
| 8 | * Please view the LICENSE distributed with this source code |
||
| 9 | * for the full copyright and license information. |
||
| 10 | * |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Koded\Session; |
||
| 14 | |||
| 15 | use Koded\Stdlib\{Config, Immutable}; |
||
| 16 | use Koded\Stdlib\Interfaces\ConfigurationFactory; |
||
| 17 | |||
| 18 | |||
| 19 | class SessionConfiguration extends Config |
||
| 20 | { |
||
| 21 | |||
| 22 | 68 | public function __construct(ConfigurationFactory $settings) |
|
| 23 | { |
||
| 24 | $this |
||
| 25 | 68 | ->set('name', 'session') |
|
| 26 | 68 | ->import($settings->get('session', [])) |
|
| 27 | 68 | ->import([ |
|
| 28 | 68 | 'use_strict_mode' => '1', // enable to prevent session fixation |
|
| 29 | 'use_trans_sid' => '0', // disable to prevent session fixation and hijacking |
||
| 30 | 'use_only_cookies' => '1', // disable session identifiers in the URLs |
||
| 31 | 'cache_limiter' => '', // disable response headers |
||
| 32 | 'referer_check' => '', // disable it, not a safe implementation (with substr() check) |
||
| 33 | ]); |
||
| 34 | |||
| 35 | 68 | if ($this->get('expire_at_browser_close')) { |
|
| 36 | 1 | ini_set('session.cookie_lifetime', 0); |
|
| 37 | 1 | $this->set('cookie_lifetime', 0); |
|
| 38 | } |
||
| 39 | |||
| 40 | 68 | foreach ($this as $name => $value) { |
|
| 41 | 68 | @ini_set('session.' . $name, $value); |
|
|
0 ignored issues
–
show
|
|||
| 42 | } |
||
| 43 | 68 | } |
|
| 44 | |||
| 45 | 67 | public function handler(): string |
|
| 46 | { |
||
| 47 | 67 | return $this->get('save_handler', 'files'); |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Session directives for session_start() function. |
||
| 52 | * |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | 65 | public function sessionParameters(): array |
|
| 56 | { |
||
| 57 | 65 | return (new Immutable($this->filter(ini_get_all('session', false), 'session.', false))) |
|
| 58 | 65 | ->extract([ |
|
| 59 | 65 | 'cache_expire', |
|
| 60 | 'cache_limiter', |
||
| 61 | 'gc_maxlifetime', |
||
| 62 | 'name', |
||
| 63 | 'referer_check', |
||
| 64 | 'serialize_handler', |
||
| 65 | 'sid_bits_per_character', |
||
| 66 | 'sid_length', |
||
| 67 | 'use_cookies', |
||
| 68 | 'use_only_cookies', |
||
| 69 | 'use_strict_mode', |
||
| 70 | 'use_trans_sid', |
||
| 71 | ]); |
||
| 72 | } |
||
| 73 | } |
||
| 74 |
If you suppress an error, we recommend checking for the error condition explicitly: