How can PHP be used to implement a logout function for a protected website directory?

To implement a logout function for a protected website directory in PHP, you can unset the session variables related to the user authentication and redirect them to the login page. This will effectively log the user out of the protected area.

<?php
session_start();

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

// Destroy the session
session_destroy();

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