How can PHP sessions be used to improve security in user login systems?
Using PHP sessions can improve security in user login systems by storing sensitive user information on the server-side rather than on the client-side. This helps prevent unauthorized access to user data and reduces the risk of session hijacking. Additionally, PHP sessions provide a built-in way to manage user authentication and authorization, making it easier to implement secure login functionality.
// Start a session
session_start();
// Set session variables
$_SESSION['user_id'] = $user_id;
$_SESSION['username'] = $username;
// Verify session on protected pages
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}