db = $dbConn` constructor got wrong. zmDbConnOrNull() // returns null when the database is unreachable and the methods below degrade // to "no session" rather than ending the request. // SessionUpdateTimestampHandlerInterface is what makes PHP call validateId(): // without it session.use_strict_mode has no effect for a user handler, and an // id the client made up became its session id. class ZMSessionHandler implements SessionHandlerInterface, SessionUpdateTimestampHandlerInterface { // Access time of the stored row read() found, and the id it belongs to. private $readId = null; private $readAccess = null; public function open($path, $name): bool { return zmDbConnOrNull() ? true : false; } public function close() : bool { // The example code closed the db connection.. I don't think we care to. return true; } #[\ReturnTypeWillChange] public function read($id){ if (!($db = zmDbConnOrNull())) return ''; $sth = $db->prepare('SELECT data, access FROM Sessions WHERE id = :id'); if (!$sth->bindParam(':id', $id, PDO::PARAM_STR, 32)) { ZM\Error("Failed to bind param"); if (!$sth->bindParam(':id', $id, PDO::PARAM_STR)) { ZM\Error("Failed to bind param"); } } if ( $sth->execute() ) { if (( $row = $sth->fetch(PDO::FETCH_ASSOC) ) ) { $this->readId = $id; $this->readAccess = intval($row['access']); return $row['data']; } } // Return an empty string return ''; } public function write($id, $data) : bool { if (!zm_session_is_persistable()) return true; if (!($db = zmDbConnOrNull())) return false; // Create time stamp $access = time(); $sth = $db->prepare('REPLACE INTO Sessions VALUES (:id, :access, :data)'); $sth->bindParam(':id', $id, PDO::PARAM_STR, 32); $sth->bindParam(':access', $access, PDO::PARAM_INT); $sth->bindParam(':data', $data); return $sth->execute() ? true : false; } public function destroy($id) : bool { if (!($db = zmDbConnOrNull())) return false; $sth = $db->prepare('DELETE FROM Sessions WHERE Id = :id'); $sth->bindParam(':id', $id, PDO::PARAM_STR, 32); return $sth->execute() ? true : false; } #[\ReturnTypeWillChange] public function gc($max) { if (!($db = zmDbConnOrNull())) return false; // Calculate what is to be deemed old $now = time(); $old = $now - $max; ZM\Debug('doing session gc ' . $now . '-' . $max. '='.$old); // Two-phase delete: find expired ids via the access index (consistent read, no locks), // then delete by primary key so InnoDB only takes record locks on the matched rows // and not gap locks across the access range — avoids deadlocks with concurrent // REPLACE INTO Sessions on every authenticated request. $sel = $db->prepare('SELECT id FROM Sessions WHERE access < :old LIMIT 100'); $sel->bindParam(':old', $old, PDO::PARAM_INT); if (!$sel->execute()) return false; $ids = $sel->fetchAll(PDO::FETCH_COLUMN); if (!$ids) return true; $placeholders = implode(',', array_fill(0, count($ids), '?')); $del = $db->prepare("DELETE FROM Sessions WHERE id IN ($placeholders)"); return $del->execute($ids) ? true : false; } // Whether $id names a stored session. With session.use_strict_mode (set in // zm_session_start()) PHP replaces an id that doesn't with a new one, so a // client can't choose its session id and nobody can plant one (session // fixation). public function validateId($id) : bool { if (!($db = zmDbConnOrNull())) return false; $sth = $db->prepare('SELECT 1 FROM Sessions WHERE id = :id'); $sth->bindParam(':id', $id, PDO::PARAM_STR, 32); return $sth->execute() and ($sth->fetch() !== false); } // Called instead of write() when the session data didn't change. public function updateTimestamp($id, $data) : bool { if (($this->readId === $id) && ($this->readAccess !== null) && ((time() - $this->readAccess) < 60)) { return true; } return $this->write($id, $data); } } # end class Session $session = new ZMSessionHandler; session_set_save_handler($session, true); ?>