Files
BiveEngine/engine/Main/trait.sessions.php
2025-12-24 19:19:01 +03:00

43 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// Работа с сессиями
defined('ROOT_DIR') || exit;
trait Sessions {
// Создать сессию для посетителя
public function session_create()
{
if(isset($_COOKIE["b_session"])) return true;
$session_id = bin2hex(random_bytes(16));
setcookie('b_session', $session_id, time() + (30 * 24 * 60 * 60), '/', '', SESSION_SECURE, true);
$_COOKIE["b_session"] = $session_id;
return $session_id;
}
// Получить значение сессии
public function session_get_name()
{
$session_name = $_COOKIE["b_session"];
if(!isset($session_name)) return $this->session_create();
return $session_name;
}
// Получить значение сессии
public function session_get($key)
{
$session_name = $this->session_get_name();
$keys = $this->db_query("SELECT * FROM `bive_session` WHERE `session` = ? AND `key` = ?", array($session_name, $key), true);
if(!isset($keys[0])) return false;
return $keys[0]["value"];
}
// Сохранить значение сессии
public function session_set($key, $value)
{
$session_name = $this->session_get_name();
$old_value = $this->session_get($key);
if($old_value === false) return $this->db_query("INSERT INTO `bive_session`(`session`, `key`, `value`) VALUES (?, ?, ?)", array($session_name, $key, $value), true);
return $this->db_query("UPDATE `bive_session` SET `value` = ? WHERE `session` = ? AND `key` = ?", array($value, $session_name, $key), true);
}
}