title: How to set cookie after user submits form via $_POST method with login credentials (password and username)?
body: <p>Suppose a user inputs his/her username and password and clicks on the submit button, which utilizes $_POST method on the form. To log in successfully, obviously the username and password have to match what's in my mysql database, which is done through a series of "if" statements. The form and "if" statements have to lie within the html tags to display the correct error messages if the credentials are wrong. After the username and password successfully satisfy all of the "if" statements, which are located within the html tags, I obviously want to set a cookie. However, I can't set a cookie within the html tags.</p>
  
  <pre><code>/*setcook() function can ONLY be placed HERE before the &lt;html&gt; tags, but that does not work with my php code*/
  &lt;html&gt;
  &lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;body&gt;
  
  &lt;?php
  if (isset($_POST['username']) OR isset($_POST['password']))
  { 
  /*bunch of "if" statements go here to confirm the credentials are correct and match what's in the database. if the username and password are correct, all of the "if" statements here are passed, and then i WANT to set a cookie HERE so the user is logged in but i can't*/
  }
  
  else echo &lt;&lt;&lt;_END
  &lt;form action="login.php" method="post"&gt;
  Username: &lt;input type="text" name="username"/&gt;
  Password: &lt;input type="password" name="password" /&gt;
  &lt;input type="submit" value="Submit" /&gt;"; //this is the form that the user fills out and submits
  &lt;/form&gt;
  _END;
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>HOWEVER, the setcookie() function only works BEFORE the html tag. How can I set a cookie AFTER all the username and password credentials are verified in my PHP code that lies inside the html tags?</p>