eesyphp/includes/session.php

47 lines
1.3 KiB
PHP
Raw Normal View History

2020-11-18 18:53:47 +01:00
<?php
use EesyPHP\Log;
2020-11-18 18:53:47 +01:00
if (php_sapi_name() == "cli")
return true;
// Define session max duration
if (!isset($session_max_duration))
2022-04-24 17:43:44 +02:00
$session_max_duration = (12*60*60); // Default to 12h
2020-11-18 18:53:47 +01:00
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'])) {
2022-04-24 17:43:44 +02:00
$_SESSION['session_key']=uniqid();
2020-11-18 18:53:47 +01:00
}
// Handle session timeout
if (isset($session_timeout) && $session_timeout) {
2022-04-24 17:43:44 +02:00
if (!isset($_SESSION['session_last_access'])) {
Log :: debug('Set initial session last access');
2022-04-24 17:43:44 +02:00
$_SESSION['session_last_access'] = time();
}
elseif ($_SESSION['session_last_access'] > (time() - $session_timeout)) {
Log :: debug(
2022-04-24 17:43:44 +02:00
'Session timeout not expired, update session last access '.
'(Previous value : '.$_SESSION['session_last_access'].')');
$_SESSION['session_last_access'] = time();
}
else {
Log :: info('Session destroyed due to inactivity');
2022-04-24 17:43:44 +02:00
session_destroy();
}
2020-11-18 18:53:47 +01:00
}
function check_session_key($value=null) {
if (is_null($value) && isset($_REQUEST['session_key']))
$value = $_REQUEST['session_key'];
return ($value && $_SESSION['session_key'] == $value);
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab