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

77 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit 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 = 'BLUDIT-KEY';
2015-03-08 18:02:59 +01:00
2015-03-27 02:00:01 +01:00
public static function start()
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 TRUE cookie will only be sent over secure connections.
$secure = false;
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();
2015-09-08 02:51:48 +02:00
session_set_cookie_params(
2017-10-13 00:15:13 +02:00
SESSION_COOKIE_LIFE_TIME,
2015-09-08 02:51:48 +02:00
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly
);
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;
}
}