report_mode = MYSQLI_REPORT_OFF; $connection = new mysqli( MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE, MYSQL_PORT ); if ($connection->connect_error) die("Error connecting to a database."); // Custom session handler functions class MySQLSessionHandler { private $mysqli; public function __construct($mysqli) { $this->mysqli = $mysqli; } public function open($savePath, $sessionName) { return true; } public function close() { return true; } public function read($id) { $data = null; $stmt = $this->mysqli->prepare("SELECT data FROM sessions WHERE id = ?"); if (!$stmt) return ''; $stmt->bind_param('s', $id); $stmt->execute(); $stmt->bind_result($data); $stmt->fetch(); $stmt->close(); return isset($data) && $data ? $data : ''; } public function write($id, $data) { $stmt = $this->mysqli->prepare("REPLACE INTO sessions (id, data) VALUES (?, ?)"); if (!$stmt) return false; $stmt->bind_param('ss', $id, $data); return $stmt->execute(); } public function destroy($id) { $stmt = $this->mysqli->prepare("DELETE FROM sessions WHERE id = ?"); if (!$stmt) return false; $stmt->bind_param('s', $id); return $stmt->execute(); } public function gc($maxlifetime) { return true; } public function create_sid() { if (function_exists('random_bytes')) { $sid = bin2hex(random_bytes(32)); } else { $sid = ''; for ($i = 0; $i < 32; $i++) { $sid = $sid . bin2hex(rand(0, 255)); } } return $sid; } public function validate_sid($key) { $stmt = $this->mysqli->prepare("SELECT data FROM sessions WHERE id = ?"); if (!$stmt) return false; $stmt->bind_param('s', $key); $stmt->execute(); $result = $stmt->get_result(); if (!$result) { $stmt->close(); return false; } else { $valid = boolval($result->fetch_assoc()); $stmt->close(); return $valid; } } }