title: php set cookie redirect
body: <p>I need a domain example.com redirects to sub-domain a.example.com when I type on the address bar. </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(function(){
      var city = readCookie('city');
      if(city==null &amp;&amp; city==''){
      window.location.href = 'http://' + city + '.example.com';
  }
  $('#citygo').change(function(){
      var city = $(this).val();
      createCookie('city', city, 28);
      window.location.href = 'http://' + city + '.example.com';
  });
  });
  &lt;/script&gt;
  
  &lt;body&gt;
  &lt;select id="citygo"&gt;
      &lt;option value="0"&gt;Select City&lt;/option&gt;
      &lt;option value="amsterdam"&gt;Amsterdam&lt;/option&gt;
      &lt;option value="newyork"&gt;New York&lt;/option&gt;
      &lt;option value="london"&gt;London&lt;/option&gt;
      &lt;option value="cardiff"&gt;Cardiff&lt;/option&gt;
      &lt;/select&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>The cookie on the server side is not holding so the domain cannot remember sub-domain. What am i doing wrong? Any help will be very much appreciated.</p>
  
  <pre><code>&lt;?php 
  $hour = time() + 50400; 
  setcookie(My_Site_Location, $_POST['citygo'], $hour); 
  
  $Loc=$_COOKIE["city"]; 
  if(isset($_POST['city'])) 
  $Loc=$_POST['city']; 
  
  if (empty($Loc)) { 
      header("Location: http://{$_COOKIE["city"]}.example.com");  
      } else { 
      header("Location: example.com/$Loc.php");  
  } 
  ?&gt;
  </code></pre>