How can PHP be used to automatically redirect users based on their previous selections stored in cookies?

To automatically redirect users based on their previous selections stored in cookies, you can use PHP to check the cookie values and then redirect the user accordingly. By checking the cookie values and using a conditional statement, you can determine where the user should be redirected based on their previous selections.

<?php
// Check if the cookie is set
if(isset($_COOKIE['previous_selection'])) {
    $previousSelection = $_COOKIE['previous_selection'];
    
    // Redirect the user based on their previous selection
    switch($previousSelection) {
        case 'selection1':
            header('Location: page1.php');
            break;
        case 'selection2':
            header('Location: page2.php');
            break;
        default:
            header('Location: default.php');
            break;
    }
} else {
    // Redirect to a default page if the cookie is not set
    header('Location: default.php');
}
?>