In PHP, what are the recommended methods for handling user authentication and session management to prevent unauthorized access to protected pages?

To handle user authentication and session management in PHP, it is recommended to use functions like password_hash() and password_verify() for securely storing and verifying passwords. Additionally, PHP sessions can be used to manage user sessions and restrict access to protected pages by checking if the user is authenticated.

// Start a session
session_start();

// Check if user is not authenticated, redirect to login page
if (!isset($_SESSION['authenticated'])) {
    header('Location: login.php');
    exit();
}

// Verify user authentication
if ($_SESSION['authenticated'] !== true) {
    header('Location: login.php');
    exit();
}