What are the best practices for handling session variables and cookies in PHP, especially when it comes to logout mechanisms and user authentication?

When handling session variables and cookies in PHP, it's important to properly manage user authentication and logout mechanisms to ensure the security of user data. One best practice is to unset session variables and destroy the session upon logout to prevent unauthorized access. Additionally, cookies should be properly set with secure and httponly flags to prevent cross-site scripting attacks.

// Logout mechanism to unset session variables and destroy the session
session_start();
$_SESSION = array();
session_destroy();

// Set a cookie with secure and httponly flags
setcookie("cookie_name", "cookie_value", time() + 3600, "/", "example.com", true, true);