title: PHP Login & Remember Me
body: <p>Evening all,</p>
  
  <p>I'm having a nightmare with cookies. I've tried a million and one ways to keep users logged in but fail.</p>
  
  <p>My login.php</p>
  
  <pre><code>&lt;?php
  require_once('config.php');
  if( $user-&gt;is_logged_in() ){ header('Location: index.php'); exit(); }
  if(isset($_POST['submit'])){
      if (!isset($_POST['username'])) $error[] = "Please fill out all fields";
      if (!isset($_POST['password'])) $error[] = "Please fill out all fields";
      $username = $_POST['username'];
      if ( $user-&gt;isValidUsername($username)){
          if (!isset($_POST['password'])){
              $error[] = 'A password must be entered';
          }
          $password = $_POST['password'];
          if($user-&gt;login($username,$password)){
              $_SESSION['username'] = $username;
              header('Location: index.php');
              exit;
          } else {
              $error[] = 'Wrong username or password or your account has not been activated.';
          }
      }else{
          $error[] = 'Usernames are required to be Alphanumeric, and between 3-16 characters long';
      }
  }
  ?&gt;
  </code></pre>
  
  <p>and on the pages that need the user to be logged currently using at the top of the page.</p>
  
  <pre><code>if( $user-&gt;is_logged_in() ){ header('Location: profile.php'); exit(); }
  </code></pre>
  
  <p>Any advice for how I can add cookies to my existing script? I'm not highly skilled in PHP so looking for something basic but secure. Thanks all!</p>