What are the best practices for implementing user authentication in PHP, particularly in terms of session management?

Issue: Implementing user authentication in PHP requires proper session management to ensure secure and reliable user login functionality. Code snippet:

```php
session_start();

// Check if user is logged in
if(isset($_SESSION['user_id'])) {
    // User is already logged in
    // Redirect to dashboard or home page
    header("Location: dashboard.php");
    exit();
}

// Validate user credentials (e.g., username and password)
if($_POST['username'] == 'admin' && $_POST['password'] == 'password') {
    // Authentication successful
    $_SESSION['user_id'] = 1; // Set user ID in session
    header("Location: dashboard.php");
    exit();
} else {
    // Authentication failed
    echo "Invalid username or password";
}
```

This code snippet demonstrates a basic user authentication implementation in PHP. It starts the session, checks if the user is already logged in, validates user credentials, and sets the user ID in the session upon successful authentication. Additionally, it handles redirecting the user to the dashboard page upon successful login or displaying an error message for failed authentication.