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;
?>
Related Questions
- How can PHP developers improve the security of their code by disabling register_globals and using the POST method instead of GET?
- What are some best practices for handling file manipulation in PHP, such as renaming and deleting files?
- What are the best practices for organizing JavaScript code in a separate file instead of embedding it in HTML for PHP applications?