What is the purpose of session_destroy() in PHP and why is it not recommended for logging out users?
The purpose of session_destroy() in PHP is to destroy all data associated with the current session. However, it is not recommended for logging out users because it destroys all session data, including variables that may be needed for other functionality. Instead, it is better to unset specific session variables related to user authentication.
// Unset specific session variables for logging out users
session_start();
unset($_SESSION['user_id']);
unset($_SESSION['username']);
// Redirect to login page or home page
header("Location: login.php");
exit();
Keywords
Related Questions
- What could be the potential reasons for the fatal error "Interface 'User\Auth\DbBcryptAdapterInterface' not found" in a PHP project?
- How can the use of sessions in PHP improve the management of a shopping cart feature in an online shop script?
- What are the best practices for setting file permissions in PHP when using suPHP?