Compare commits

...

1 Commits

Author SHA1 Message Date
344786ee95 fixed up session class 2025-08-14 20:19:24 -07:00

View File

@ -4,20 +4,26 @@ class Session {
private $session;
public function __construct() {
session_start();
if (!isset($_SESSION['token'])) {
$this->setToken();
$this->session['messages'] = [];
} else {
$this->session = $_SESSION;
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
$this->session = &$_SESSION; // Reference $_SESSION to keep them in sync
if (!isset($this->session['token'])) {
$this->setToken();
}
if (!isset($this->session['messages'])) {
$this->session['messages'] = []; // Always ensure messages is an array
}
if (!isset($this->session['formData'])) {
$this->session['formData'] = []; // Initialize formData
}
if (!isset($this->session['errors'])) {
$this->session['errors'] = []; // Initialize errors
}
}
public function setToken() {
if (!isset($this->session['token'])) {
$this->session['token'] = bin2hex(random_bytes(32));
}
$this->session['token'] = bin2hex(random_bytes(32));
}
public function set($key, $value) {
@ -45,13 +51,13 @@ class Session {
}
public function write() {
$_SESSION = $this->session;
// No need to assign to $_SESSION since $this->session is a reference
session_write_close();
}
public function kill() {
$this->session = [];
$_SESSION = [];
session_destroy();
}
}