koblog/bl-kernel/helpers/session.class.php

86 lines
1.9 KiB
PHP
Raw Normal View History

<?php defined('KOBLOG') or die('Koblog CMS.');
2015-03-08 18:02:59 +01:00
2018-07-25 23:42:00 +02:00
class Session {
2015-03-08 18:02:59 +01:00
2015-03-27 02:00:01 +01:00
private static $started = false;
private static $sessionName = 'KOBLOG-KEY';
2015-03-08 18:02:59 +01:00
2021-11-28 12:45:31 +01:00
public static function start($path, $secure)
2015-03-08 18:02:59 +01:00
{
2017-10-13 00:15:13 +02:00
// Try to set the session timeout on server side, 1 hour of timeout
ini_set('session.gc_maxlifetime', SESSION_GC_MAXLIFETIME);
2015-05-05 03:00:01 +02:00
2015-09-08 02:51:48 +02:00
// If set to TRUE then PHP will attempt to send the httponly flag when setting the session cookie.
$httponly = true;
2015-05-05 03:00:01 +02:00
// Gets current cookies params.
$cookieParams = session_get_cookie_params();
2021-11-28 12:45:31 +01:00
if (empty($path)) {
$httponly = true;
$path = '/';
}
session_set_cookie_params([
'lifetime' => $cookieParams["lifetime"],
'path' => $path,
'domain' => $cookieParams["domain"],
'secure' => $secure,
'httponly' => true
]);
2015-05-05 03:00:01 +02:00
2015-09-08 02:51:48 +02:00
// Sets the session name to the one set above.
session_name(self::$sessionName);
2015-05-15 00:07:45 +02:00
2015-09-08 02:51:48 +02:00
// Start session.
self::$started = session_start();
2015-05-05 03:00:01 +02:00
// Regenerated the session, delete the old one. There are problems with AJAX.
//session_regenerate_id(true);
2015-06-27 03:47:12 +02:00
2017-10-13 00:15:13 +02:00
if (!self::$started) {
2015-09-08 02:51:48 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to start the session.');
}
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
public static function started()
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
return self::$started;
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
public static function destroy()
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
session_destroy();
unset($_SESSION);
unset($_COOKIE[self::$sessionName]);
Cookie::set(self::$sessionName, '', -1);
2015-03-27 02:00:01 +01:00
self::$started = false;
2015-06-27 03:47:12 +02:00
Log::set(__METHOD__.LOG_SEP.'Session destroyed.');
2015-05-15 00:07:45 +02:00
return !isset($_SESSION);
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
public static function set($key, $value)
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
$key = 's_'.$key;
2015-05-15 00:07:45 +02:00
2015-03-27 02:00:01 +01:00
$_SESSION[$key] = $value;
2015-03-08 18:02:59 +01:00
}
2015-03-27 02:00:01 +01:00
public static function get($key)
2015-03-08 18:02:59 +01:00
{
2015-03-27 02:00:01 +01:00
$key = 's_'.$key;
2015-03-08 18:02:59 +01:00
if (isset($_SESSION[$key])) {
2015-03-27 02:00:01 +01:00
return $_SESSION[$key];
2015-06-27 03:47:12 +02:00
}
2015-03-08 18:02:59 +01:00
return false;
}
2021-11-28 12:45:31 +01:00
public static function remove($key)
{
$key = 's_'.$key;
2021-11-28 12:45:31 +01:00
unset($_SESSION[$key]);
}
}