SessionHandlerInterface::destroy
Destroy a session
Function name: SessionHandlerInterface::destroy()
Function Description: This function is used to destroy the specified session.
Applicable version: PHP 5 >= 5.4.0, PHP 7
Syntax: public SessionHandlerInterface::destroy(string $session_id): bool
parameter:
Return value:
Example:
<?php // 自定义会话处理程序类class MySessionHandler implements SessionHandlerInterface { // 实现destroy()函数public function destroy($session_id) { // 在这里编写销毁会话的代码// 例如,可以从数据库或文件系统中删除会话数据if ($session_id != '') { // 销毁会话的处理逻辑// 例如,可以使用数据库查询删除会话数据$db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password'); $stmt = $db->prepare('DELETE FROM sessions WHERE session_id = :session_id'); $stmt->bindParam(':session_id', $session_id); $stmt->execute(); // 返回true表示销毁成功return true; } // 返回false表示销毁失败return false; } // 其他SessionHandlerInterface函数的实现... } // 使用自定义会话处理程序$handler = new MySessionHandler(); session_set_save_handler($handler, true); // 销毁指定会话$session_id = 'abc123'; // 要销毁的会话ID $result = $handler->destroy($session_id); if ($result) { echo '会话销毁成功'; } else { echo '会话销毁失败'; } ?>
The above example demonstrates how to use the custom session handler class and the SessionHandlerInterface::destroy() function to destroy a specified session. In practical applications, you need to write code to destroy sessions based on specific needs, such as deleting session data from a database or file system. After the session is successfully destroyed, the function returns true; if it fails, false.