title: PHP SESSIONS, COOKIES and Remember Me function
body: <p>I have the following code, when a user logs in, they are presented with two text boxes and a checkbox.</p>
  
  <p>Setting the three cookies, <code>username</code>, <code>password</code> and <code>remember</code> all work and the log in script itself is comepletely fine (I appreciate that storing hashed passwords in the cookie isn't best practice but for now it will do).</p>
  
  <p>What happens though, is id the user re-visits the <code>login.php</code> page (this one) while they are already logged in the cookies are removed one-by-one as the $_POST condition is not being met and therefore the lines below are being executed. How can I prevent this from happening. Also, any suggestions to clean up the code as I will no doubt end up with a lot of repeated code will be appreciated. Thanks</p>
  
  <p><code>snippet from the 'login.php' page below</code></p>
  
  <pre><code>} elseif (!$_POST['remember']) {
      $past = time() - 100;
      if (isset($_COOKIE['remember'])) {
          setcookie('remember', '', $past);
      } elseif (isset($_COOKIE['username'])) {
          setcookie('username', '', $past);
      } elseif (isset($_COOKIE['password'])) {
          setcookie('password', '', $past);
      }
  }
  </code></pre>
  
  <p><code>login.php</code></p>
  
  <pre><code>&lt;?php
  session_start();
  include("includes/config.php");
  
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Login&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;?php
  
  $odb = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
  
  $username = "";
  $password = "";
  
  if (isset($_COOKIE['username']) &amp;&amp; isset($_COOKIE['password'])) {
  
      $username = $_COOKIE['username'];
      $password = $_COOKIE['password'];
  
  } elseif (isset($_POST['username'])) {
  
      $username = $_POST['username'];
      $password = $_POST['password'];
      $password = md5(DB_SALT.$password);
  
  }
  
  $sql = "SELECT * from tblMembers WHERE username = :username";
  $query = $odb-&gt;prepare($sql);
  $query-&gt;execute(array(":username" =&gt; $username));
  $results = $query-&gt;fetchAll();
  if($results !== FALSE &amp;&amp; $query-&gt;rowCount()&gt;0) {
      if($results[0]['passwordHash'] == $password) {
          $_SESSION['username'] = $username;
          $_SESSION['userID'] = $results[0]['userID'];
  
          if($_POST['remember']) {
              $month = time() + (60 * 60 * 24 * 30);
              setcookie('remember', $_POST['username'], $month);
              setcookie('username', $_POST['username'], $month);
              setcookie('password', $results[0]['passwordHash'], $month);
          } elseif (!$_POST['remember']) {
              $past = time() - 100;
              if (isset($_COOKIE['remember'])) {
                  setcookie('remember', '', $past);
              } elseif (isset($_COOKIE['username'])) {
                  setcookie('username', '', $past);
              } elseif (isset($_COOKIE['password'])) {
                  setcookie('password', '', $past);
              }
          }
  
  
          header("Location: "."index.php");
      } else {
          echo "password incorrect";
      }
  }
  ?&gt;
  &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']?&gt;" method="post"&gt;
  Username:&amp;nbsp;
  &lt;?php
  if(isset($_COOKIE['username'])) {
      echo "&lt;input type=\"text\" id=\"username\" name=\"username\" maxlength=\"40\" value=".$_COOKIE['username']."&gt;";
  } else {
      echo "&lt;input type=\"text\" id=\"username\" name=\"username\" maxlength=\"40\" value=\"\"&gt;";
  }
  ?&gt;
  Password:&amp;nbsp;&lt;input type="password" id="password" name="password" maxlength="50"&gt;
  Remember Me:&amp;nbsp;
  &lt;?php
  if(isset($_COOKIE['remember'])) {
      echo "&lt;input type=\"checkbox\" id=\"remember\" name=\"remember\" checked=\"checked\"&gt;";
  } else {
      echo "&lt;input type=\"checkbox\" id=\"remember\" name=\"remember\"&gt;";
  }
  ?&gt;
  &lt;input type="submit" id="submit" name="submit" value="Log In"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>