Are there any best practices for handling session variables and cookies in PHP logout scripts?

When logging out a user in PHP, it is important to properly unset or destroy session variables and clear any associated cookies to ensure that the user is fully logged out and their session is terminated.

// Start the session
session_start();

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

// Destroy the session
session_destroy();

// Clear any cookies related to the session
setcookie(session_name(), '', time() - 3600, '/');

// Redirect the user to the login page or any other desired page
header("Location: login.php");
exit();