title: Creating cookies in php
body: <p>I am trying to learn how to use cookies from <a href="http://www.phpnerds.com/article/using-cookies-in-php/2" rel="nofollow">PHPNerds</a>. I am having trouble in running the scripts that they have mentioned(I almost understand what the code does but I am unable to get which code will be stored with which name ). They are as below,</p>
  
  <h2>User Login</h2>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;User Logon&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h2&gt;User Login &lt;/h2&gt;
    &lt;form name="login" method="post" action="login.php"&gt;
     Username: &lt;input type="text" name="username"&gt;&lt;br&gt;
     Password: &lt;input type="password" name="password"&gt;&lt;br&gt;
     Remember Me: &lt;input type="checkbox" name="rememberme" value="1"&gt;&lt;br&gt;
     &lt;input type="submit" name="submit" value="Login!"&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h2>Login Code</h2>
  
  <pre><code>&lt;?php
  /* These are our valid username and passwords */
  $user = 'jonny4';
  $pass = 'delafoo';
  
  if (isset($_POST['username']) &amp;&amp; isset($_POST['password')) {
  
      if (($_POST['username'] == $user) &amp;&amp; ($_POST['password'] == $pass)) {    
  
          if (isset($_POST['rememberme'])) {
              /* Set cookie to last 1 year */
              setcookie('username', $_POST['username'], time()+60*60*24*365, '/account', 'www.example.com');
              setcookie('password', md5($_POST['password']), time()+60*60*24*365, '/account', 'www.example.com');
  
          } else {
              /* Cookie expires when browser closes */
              setcookie('username', $_POST['username'], false, '/account', 'www.example.com');
              setcookie('password', md5($_POST['password']), false, '/account', 'www.example.com');
          }
          header('Location: index.php');
  
      } else {
          echo 'Username/Password Invalid';
      }
  
  } else {
      echo 'You must supply a username and password.';
  }
  ?&gt;
  </code></pre>
  
  <h2>Validating</h2>
  
  <pre><code>&lt;?php
  /* These are our valid username and passwords */
  $user = 'jonny4';
  $pass = 'delafoo';
  
  if (isset($_COOKIE[['username']) &amp;&amp; isset($_COOKIE['password')) {
  
      if (($_POST['username'] != $user) || ($_POST['password'] != md5($pass))) {    
          header('Location: login.html');
      } else {
          echo 'Welcome back ' . $_COOKIE['username'];
      }
  
  } else {
      header('Location: login.html');
  }
  ?&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>