What are some best practices for implementing a user logout functionality in PHP to ensure data integrity and security?

When implementing a user logout functionality in PHP, it is important to ensure data integrity and security by properly destroying the session and clearing any sensitive information stored in session variables. This can help prevent unauthorized access to user data even after they have logged out.

// Start the session
session_start();

// Unset all session variables
$_SESSION = array();

// Destroy the session
session_destroy();

// Redirect to the login page
header("Location: login.php");
exit();