title: PHP – setcookie() not working
body: <p>I have this page that sets a cookie and echos out a string if you check a checkbox. The string prints correctly, but the cookie never gets set and I have no idea why.</p>
  
  <pre><code>&lt;form action="&lt;?php echo $_SERVER['PHP_SELF'] ?&gt;" method="post"&gt;
  &lt;label for="checkbox"&gt;Option 1:&lt;/label&gt;
  &lt;input type="checkbox" name="checkbox" id="checkbox"&gt;&lt;br&gt;
  &lt;input type="submit" name="submit" value="Submit"&gt;
  &lt;/form&gt;
    &lt;?php
  if (isset($_POST['checkbox'])) {
    setcookie("cookie", "on", time()+3600*24);
    echo "You checked the checkbox and a cookie was set with a value of:&lt;br&gt;";
  }
  else {
    setcookie("cookie", "off", time()+3600*24);
    echo "You didn't check the checkbox and a cookie was set with a value of:&lt;br&gt;";
  }
  echo $_COOKIE['cookie'];
    ?&gt;
  </code></pre>
  
  <p>Does anyone know why the above code does not work?</p>