diff --git a/src/Session.php b/src/Session.php index 3053e29..dfa9d7e 100644 --- a/src/Session.php +++ b/src/Session.php @@ -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(); } - } \ No newline at end of file