From fe19313c2fbbf2d08b40252c69ef8843d97e61d7 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 7 Mar 2023 18:29:34 +0100 Subject: [PATCH] Session: add directory_path config parameter --- example/data/sessions/.gitignore | 1 + example/includes/config.yml | 8 ++++++-- skel/config.yml | 8 ++++++-- skel/data/sessions/.gitignore | 1 + src/Session.php | 11 +++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 example/data/sessions/.gitignore create mode 100644 skel/data/sessions/.gitignore diff --git a/example/data/sessions/.gitignore b/example/data/sessions/.gitignore new file mode 100644 index 0000000..54d1952 --- /dev/null +++ b/example/data/sessions/.gitignore @@ -0,0 +1 @@ +sess_* diff --git a/example/includes/config.yml b/example/includes/config.yml index 02aef4e..2ff7dec 100644 --- a/example/includes/config.yml +++ b/example/includes/config.yml @@ -102,8 +102,12 @@ i18n: # Session # session: - timeout: 1800 # Session timeout dur to inactivity (in seconds) - max_duration: 43200 # Session max duration (in seconds, default : 12h) + # Session timeout due to inactivity (in seconds) + timeout: 1800 + # Session max duration (in seconds, default : 12h) + max_duration: 43200 + # Directory path where to store PHP sessions data (optional, default: use system default) + directory_path: "${data_directory}/sessions" # # Database configuration diff --git a/skel/config.yml b/skel/config.yml index e4b4007..2bf9157 100644 --- a/skel/config.yml +++ b/skel/config.yml @@ -102,8 +102,12 @@ i18n: # Session # session: - timeout: 1800 # Session timeout dur to inactivity (in seconds) - max_duration: 43200 # Session max duration (in seconds, default : 12h) + # Session timeout due to inactivity (in seconds) + timeout: 1800 + # Session max duration (in seconds, default : 12h) + max_duration: 43200 + # Directory path where to store PHP sessions data (optional, default: use system default) + directory_path: "${data_directory}/sessions" # # Database configuration diff --git a/skel/data/sessions/.gitignore b/skel/data/sessions/.gitignore new file mode 100644 index 0000000..54d1952 --- /dev/null +++ b/skel/data/sessions/.gitignore @@ -0,0 +1 @@ +sess_* diff --git a/src/Session.php b/src/Session.php index fed7fcb..42036a7 100644 --- a/src/Session.php +++ b/src/Session.php @@ -30,6 +30,7 @@ class Session { array( 'max_duration' => 43200, // 12h 'timeout' => null, + 'directory_path' => null, ) ); @@ -38,6 +39,16 @@ class Session { Config :: ini_set('session.gc_maxlifetime', strval(self :: $max_duration)); Config :: ini_set('session.cookie_lifetime', strval(self :: $max_duration)); + // Set custom session directory (if configured) + $directory_path = App::get('session.directory_path', null, 'string'); + if ($directory_path) { + Log :: trace('Set session directory path as "%s"', $directory_path); + if (!session_save_path($directory_path)) { + Log :: warning('Fail to set session directory path as "%s"', $directory_path); + } + } + Log :: debug('Use session directory "%s"', session_save_path()); + // Start session session_start();