What are some best practices for handling user authentication and redirection in PHP applications?

When handling user authentication and redirection in PHP applications, it is important to securely authenticate users before granting access to protected resources and redirect them to appropriate pages based on their authentication status.

// Check if user is authenticated
session_start();
if(!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// Redirect user to dashboard if authenticated
header("Location: dashboard.php");
exit();