How can header redirection in PHP affect the functionality of session destruction during logout processes?

When using header redirection in PHP for logout processes, it can sometimes interfere with the functionality of session destruction. This is because the redirection might occur before the session is properly destroyed, leading to potential security risks. To solve this issue, you can use session_unset() and session_destroy() functions before performing the header redirection to ensure that the session is properly destroyed before redirecting the user.

<?php
session_start();

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

// Destroy the session
session_destroy();

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