title: php pdo login failure
body: <p>I've followed an online tutorial for a simple php login and registration script, but when I tried to login it just failed, brought up my error message "login failed. please try again, or register". The registration page works in the sense that it creates a new entry in the database, but it won't redirect to the given redirection page either. Here is the code:</p>
  
  <p><strong>class.user.php</strong> (this is included once in the config file)</p>
  
  <pre><code>&lt;?php
  
  class USER {
  private $db_user;
  
  function __construct($db) {
      $this-&gt;db_user = $db;
  }
  
  public function register($u_fname,$u_lname,$u_email,$u_tel,$uname,$upass) {
  
      try {
  
          $new_password = password_hash($upass, PASSWORD_DEFAULT);
  
          $stmt = $this-&gt;db_user-&gt;prepare("INSERT INTO users(user,pass,first_name,last_name,telephone,email) VALUES(:user, :pass, :fname, :lname, :tel, :email)");
  
          $stmt-&gt;bindparam(":user", $uname);
         $stmt-&gt;bindparam(":pass", $new_password);
         $stmt-&gt;bindparam(":email", $u_email);
         $stmt-&gt;bindparam(":fname", $u_fname);
         $stmt-&gt;bindparam(":lname", $u_lname);
         $stmt-&gt;bindparam(":tel", $u_tel);          
         $stmt-&gt;execute();
  
         return $stmt;
      } catch(PDOException $e) {
          echo $e-&gt;getMessage();
      }
  }
  
  public function login($uname, $upass) {
  
      try {
  
          $stmt = $this-&gt;db_user-&gt;prepare("SELECT * FROM users WHERE user=:user LIMIT 1");
          $stmt-&gt;execute(array(':user'=&gt;$uname));
          $userRow=$stmt-&gt;fetch(PDO::FETCH_ASSOC);
          if($stmt-&gt;rowCount() &gt; 0) {
              if(password_verify($upass, $userRow['pass'])) {
                  $_SESSION['user_session'] = $userRow['id'];
                  return true;
              } else {
                  return false;
              }
          }
      } catch(PDOException $e) {
          echo $e-&gt;getMessage();
      }
  }
  
  public function is_loggedin() {
      if(isset($_SESSION['user_session'])) {
          return true;
      }
  }
  
  public function logout() {
      session_destroy();
      unset($_SESSION['user_session']);
      return true;
  }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>login.php</strong></p>
  
  <pre><code>&lt;?php
  $page_title = "Love Deals Login";
  require('inc/connect/config.php');
  include('inc/header.php');
  
  if($user-&gt;is_loggedin()!="") {
  $user-&gt;redirect('home.php');
  }
  
  if(isset($_POST['login'])) {
  $uname = $_POST['txt_uname'];
  $upass = $_POST['txt_upass'];
  
  if($user-&gt;login($uname,$upass)) {
      $user-&gt;redirect('home.php');
  } else {
      $error = "Login failed. Please try again, or register";
  }
  }
  
  ?&gt;
  
  &lt;div class="container" style="padding: 100px 0 0 0;"&gt;
  &lt;div class="form-container"&gt;
      &lt;form method="post" id="login"&gt;
          &lt;h3&gt;Please login&lt;/h3&gt;
          &lt;?php if(isset($error)) {
              ?&gt;
              &lt;div class="alert alert-danger"&gt;
                  &lt;i class="glyphicon glyphicon-warning-sign"&gt;&lt;/i&gt; &amp;nbsp; &lt;?php echo $error; ?&gt;
              &lt;/div&gt;
              &lt;?php
          }
          ?&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="text" class="form-control" name="txt_uname" placeholder="Username" required /&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="password" class="form-control" name="txt_upass" placeholder="Password" required /&gt;
          &lt;/div&gt;
          &lt;div class="clearfix"&gt;&lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;button type="submit" name="login" class="btn btn-primary pull-right"&gt;
                  &lt;i class="glyphicon glyphicon-log-in"&gt;&lt;/i&gt;&amp;nbsp;Login&lt;/button&gt;
          &lt;/div&gt;
          &lt;div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3" style="text-align: center;"&gt;
              &lt;label&gt;Don't have an account yet? &lt;a href="signup.php"&gt;Register now&lt;/a&gt;&lt;/label&gt;
          &lt;/div&gt;
      &lt;/form&gt;
   &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;footer class="navbar-fixed-bottom search-footer"&gt;
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
  
  &lt;?php include('inc/footer.php'); ?&gt;
  </code></pre>
  
  <p><strong>signup.php</strong></p>
  
  <pre><code>&lt;?php
  $page_title = "Love Deals Register";
  require('inc/connect/config.php');
  include('inc/header.php');
  
  if($user-&gt;is_loggedin()!="") {
  $user-&gt;redirect('home.php');
  }
  
  if(isset($_POST['signup'])) {
  $u_fname = trim($_POST['sign_fname']);
  $u_lname = trim($_POST['sign_lname']);
  $u_tel = trim($_POST['sign_tel']);
  $u_email = trim($_POST['sign_email']);
  $uname = trim($_POST['sign_uname']);
  $upass = trim($_POST['sign_upass']);
  
  if($u_fname=="") {
      $error[] = "please provide first name";
  } else if ($u_lname=="") {
      $error[] = "please provide last name";
  } else if($u_tel=="") {
      $error[] = "please provide contact number";
  } else if($u_email=="") {
      $error[] = "please provide an email address";
  } else if(!filter_var($u_email, FILTER_VALIDATE_EMAIL)) {
      $error[] = "please provide a valid email address";
  } else if($uname=="") {
      $error[] = "please provide username";
  } else if($upass=="") {
      $error[] = "please provide password";
  } else if(strlen($upass) &lt; 8) {
      $error[] = "Password must be at least 8 characters";
  } else {
  
      try {
          $stmt = $db-&gt;prepare("SELECT user FROM users WHERE user=:uname");
          $stmt-&gt;execute(array(':uname'=&gt;$uname));
          $row=$stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
          if($row['user']==$uname) {
              $error[] = "sorry, username already taken! please choose another";
          } else {
              if($user-&gt;register($u_fname,$u_lname,$u_tel,$u_email,$uname,$upass)) {
                  $user-&gt;redirect('signup.php?joined');
              }
          }
      } catch(PDOException $e) {
          echo $e-&gt;getMessage();
      }
  }
  }
  
  ?&gt;
  
  &lt;div class="container" style="padding: 100px 0 0 0;"&gt;
  &lt;div class="form container"&gt;
      &lt;form method="post" id="signup"&gt;
          &lt;h3&gt;Register&lt;/h3&gt;
          &lt;?php if(isset($error)) {
              foreach($error as $error) {
                  ?&gt;
                  &lt;div class="alert alert-danger"&gt;
                      &lt;i class="glyphicon glyphicon-warning-sign"&gt;&lt;/i&gt; &amp;nbsp; &lt;?php echo $error; ?&gt;
                  &lt;/div&gt;
                  &lt;?php
              }
          } else if(isset($_GET['joined'])) {
              ?&gt;
              &lt;div class="alert alert-info"&gt;
                  &lt;i class="glyphicon glyphicon-log-in"&gt;&lt;/i&gt; &amp;nbsp; Thanks! You are now registered. &lt;a href="login.php"&gt;Login&lt;/a&gt;
              &lt;/div&gt;
              &lt;?php
          }
          ?&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="text" class="form-control" name="sign_fname" placeholder="First Name" required /&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="text" class="form-control" name="sign_lname" placeholder="Last Name" required /&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="text" class="form-control" name="sign_tel" placeholder="Contact Number" required /&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="text" class="form-control" name="sign_email" placeholder="Email" value="&lt;?php if(isset($error)){echo $umail;}?&gt;" required /&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="text" class="form-control" name="sign_uname" placeholder="Username" value="&lt;?php if(isset($error)){echo $uname;}?&gt;" required /&gt;
          &lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3 login"&gt;
              &lt;input type="password" class="form-control" name="sign_upass" placeholder="Password" required /&gt;
          &lt;/div&gt;
          &lt;div class="clearfix"&gt;&lt;/div&gt;
          &lt;div class="form-group col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3"&gt;
              &lt;button type="submit" class="btn btn-primary center-block" name="signup"&gt;
              &lt;i class="glyphicon glyphicon-open-file"&gt;&lt;/i&gt;&amp;nbsp;Register&lt;/button&gt;
          &lt;/div&gt;
          &lt;div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2 col-lg-6 col-lg-offset-3" style="text-align: center;"&gt;
              &lt;label&gt;Already have an account? &lt;a href="login.php"&gt;Login&lt;/a&gt;&lt;/label&gt;
          &lt;/div&gt;
      &lt;/form&gt;
   &lt;/div&gt;
  &lt;/div&gt;
  
  
  &lt;footer class="navbar-fixed-bottom search-footer"&gt;
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
  
  &lt;?php include('inc/footer.php'); ?&gt;
  </code></pre>
  
  <p>and finally, <strong>home.php</strong></p>
  
  <pre><code>&lt;?php
  $page_title = "Love Deals User Home";
  require('inc/connect/config.php');
  include('inc/header.php');
  
  if(!$user-&gt;is_loggedin()) {
  $user-&gt;redirect('login.php');
  }
  $user_id = $_SESSION['user_session'];
  $stmt = $db-&gt;prepare("SELECT * FROM users WHERE id=:user_id");
  $stmt-&gt;execute(array(":user_id"=&gt;$user_id));
  $userRow=$stmt-&gt;fetch(PDO::FETCH_ASSOC);
  ?&gt;
  
  &lt;div class="container" style="padding: 100px 0 0 0;"&gt;
  &lt;h3&gt;welcome back &lt;?php print($userRow['user']); ?&gt;&lt;/h3&gt;
  &lt;/div&gt;
  &lt;div&gt;
  &lt;label&gt;&lt;a href="logout.php?logout=true"&gt;&lt;i class="glyphicon glyphicon-log-out"&gt; logout&lt;/a&gt;&lt;/label&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I just can't see where I have a gone wrong! please be gentle... I'm very new to php.</p>
  
  <p>Thanks in advance</p>
  
  <p>Kaylee</p>