title: How can I set a tracking cookie so returning visitors are directed to a page they choose during first visit?
body: <p>My home page has 4 links to different sub-pages (page-1, page-2, etc.), when a visitor selects a page (i.e. /page-1/) I want them to be routed directly to that page on their return visit to the site. I'm trying to use a cookie to store the selected page and check the cookie on return to re-direct to the previously selected URL.</p>
  
  <pre><code>function set_pref_cookie(){
  
      $root = $_SERVER['REQUEST_URI'];
  
      if ($root !='/'){
          setcookie('pref_sel',$root, time()+60*60*24*5, "/");
      }
  
      if (isset($_COOKIE['pref_sel'])){
          header('Location:' .  $_COOKIE['pref_sel']);
          exit;
      }
  }
  
  add_action('init','set_pref_cookie');
  </code></pre>
  
  <p>This works to set the cookie, but on return to the site, I'm getting a redirect loop.</p>