What is the purpose of using header('Location: login') in PHP?

Using header('Location: login') in PHP is used to redirect the user to a different page, in this case, the login page. This is commonly used after a user successfully logs in or when a user needs to be redirected to a different page for authentication purposes. It helps in improving user experience and navigation within a website.

<?php
// Check if user is logged in
if($user_logged_in) {
    header('Location: dashboard.php');
    exit();
} else {
    header('Location: login.php');
    exit();
}
?>