Is it considered best practice to use header redirection in PHP to redirect users to another page after login?

Using header redirection in PHP to redirect users to another page after login is considered a common and effective practice. This method helps maintain clean URLs and improves security by preventing users from accessing restricted pages directly. To implement this, you can use the header() function in PHP to send a raw HTTP header to the browser, instructing it to redirect to the desired page.

<?php
// Start the session
session_start();

// Check if the user is logged in
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true) {
    // Redirect to the desired page
    header('Location: dashboard.php');
    exit();
} else {
    // Redirect to the login page if the user is not logged in
    header('Location: login.php');
    exit();
}
?>