What are the best practices for implementing a logout function in a PHP script?

When implementing a logout function in a PHP script, it is important to unset any session variables related to the user's login status and redirect them to a login page or homepage. This helps to ensure that the user is completely logged out and their session is terminated securely.

// Logout script
session_start(); // Start the session

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

// Destroy the session
session_destroy();

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