Move session stuff in dedicated file

This commit is contained in:
Benjamin Renard 2020-11-18 18:53:47 +01:00
parent 254ac22a64
commit af1d112ae5
4 changed files with 40 additions and 30 deletions

View file

@ -33,36 +33,7 @@ ini_set('upload_tmp_dir',$upload_tmp_dir);
require_once('logging.php'); require_once('logging.php');
require_once('functions.php'); require_once('functions.php');
require_once('session.php');
// Define session max duration
if (!isset($session_max_duration))
$session_max_duration = (12*60*60); // Default to 12h
ini_set('session.gc_maxlifetime', $session_max_duration);
ini_set('session.cookie_lifetime', $session_max_duration);
// Start session
session_start();
// Init session key
if (!isset($_SESSION['session_key'])) {
$_SESSION['session_key']=uniqid();
}
// Handle session timeout
if ($session_timeout) {
if (!isset($_SESSION['session_last_access'])) {
logging('DEBUG', 'Set initial session last access');
$_SESSION['session_last_access'] = time();
}
elseif ($_SESSION['session_last_access'] > (time() - $session_timeout)) {
logging('DEBUG', 'Session timeout not expired, update session last access (Previous value : '.$_SESSION['session_last_access'].')');
$_SESSION['session_last_access'] = time();
}
else {
logging('INFO', 'Session destroyed due to inactivity');
session_destroy();
}
}
// Nomenclatures // Nomenclatures
$status_list = array ( $status_list = array (

33
includes/session.php Normal file
View file

@ -0,0 +1,33 @@
<?php
if (php_sapi_name() == "cli")
return true;
// Define session max duration
if (!isset($session_max_duration))
$session_max_duration = (12*60*60); // Default to 12h
ini_set('session.gc_maxlifetime', $session_max_duration);
ini_set('session.cookie_lifetime', $session_max_duration);
// Start session
session_start();
// Init session key
if (!isset($_SESSION['session_key'])) {
$_SESSION['session_key']=uniqid();
}
// Handle session timeout
if ($session_timeout) {
if (!isset($_SESSION['session_last_access'])) {
logging('DEBUG', 'Set initial session last access');
$_SESSION['session_last_access'] = time();
}
elseif ($_SESSION['session_last_access'] > (time() - $session_timeout)) {
logging('DEBUG', 'Session timeout not expired, update session last access (Previous value : '.$_SESSION['session_last_access'].')');
$_SESSION['session_last_access'] = time();
}
else {
logging('INFO', 'Session destroyed due to inactivity');
session_destroy();
}
}

View file

@ -1,5 +1,8 @@
<?php <?php
if (php_sapi_name() == "cli")
return true;
require_once($smarty_path); require_once($smarty_path);
$smarty = new Smarty(); $smarty = new Smarty();

View file

@ -1,5 +1,8 @@
<?php <?php
if (php_sapi_name() == "cli")
return true;
function handle_homepage($url_info) { function handle_homepage($url_info) {
display_template("homepage.tpl", "Hello world !"); display_template("homepage.tpl", "Hello world !");
} }