How can the PHP header function be used for redirection after successful login?

After a successful login, you can use the PHP header function to redirect the user to a different page. This can be done by setting the Location header to the desired URL. It is important to make sure that no output is sent to the browser before using the header function to avoid any errors.

// Check if login is successful
if($login_successful) {
    // Redirect to the dashboard page
    header("Location: dashboard.php");
    exit();
}