title: Login using username or email
body: <p>I am trying to have my login page let users login using either their username or email address.</p>
  
  <p>So far, it will only allow users to login using email.  I need some fresh eyes, I can't find the problem.  I've tried removing every mention of logging in with email but still no luck, both username and email field in the table are unique, tried alternating that around.  Would anyone help?</p>
  
  <p><strong>Login Form:</strong></p>
  
  <pre><code>&lt;?php
  ini_set('display_errors', '1');
  require_once '../includes/conn.php';
  
  if($user-&gt;is_loggedin()!=""){
      $user-&gt;redirect('./index.php');
  }
  
  if(isset($_POST['login'])){
      $username = $_POST['username_email'];
      $email = $_POST['username_email'];
      $password = $_POST['password'];
  
      if($user-&gt;login($usrename,$email,$password)){
          $user-&gt;redirect('./index.php');
      }else{
          $error = "Login details provided do not match out records.&lt;br /&gt;&lt;br /&gt;";
      }
  }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;title&gt;EpicOwl UK | CMS Admin Panel Login&lt;/title&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" /&gt;
      &lt;link rel="stylesheet" type="text/css" href="../css/main.css"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="header"&gt;
      &lt;a href="index.php"&gt;&lt;img id="logo" src="../images/logo.png" /&gt;&lt;/a&gt;
      &lt;div id="navigation"&gt;
          &lt;ul&gt;
              &lt;a href="../index.php"&gt;&lt;li&gt;Home&lt;/li&gt;&lt;/a&gt;
              &lt;a href="../users/profile.php"&gt;&lt;li&gt;My Profile&lt;/li&gt;&lt;/a&gt;
              &lt;a href="./index.php"&gt;&lt;li&gt;Admin Panel&lt;/li&gt;&lt;/a&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="content"&gt;
  &lt;form method="post"&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;h2&gt;Administrator Login&lt;/h2&gt;
      &lt;?php
      if(isset($error)){
      ?&gt;
      &lt;em&gt;&lt;?php echo $error; ?&gt;&lt;/em&gt;
      &lt;?php
      }
      ?&gt;
      &lt;input type="text" name="username_email" placeholder="Username/Email" required /&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;input type="password" name="password" placeholder="Password" /&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;button type="submit" name="login"&gt;Login&lt;/button&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
      &lt;label&gt;Don't have an account?  Why not register one by clicking &lt;a href="./register.php"&gt;HERE&lt;/a&gt;&lt;/label&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  &lt;div id="footer"&gt;
      &lt;p class="copyright"&gt;&amp;copy; EpicOwl UK. All Rights Reserved.&lt;/p&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Class File:</strong></p>
  
  <pre><code>&lt;?php
  ini_set('display_errors', '1');
  class USER{
      private $db;
  
      function __construct($conn){
          $this-&gt;db = $conn;
      }
  
      public function register($username,$email,$password){
         try{
             $new_password = password_hash($password, PASSWORD_DEFAULT);
  
             $stmt = $this-&gt;db-&gt;prepare("INSERT INTO users(username,email,password)VALUES(:username, :email, :password)");
  
             $stmt-&gt;bindparam(":username", $username);
             $stmt-&gt;bindparam(":email", $email);
             $stmt-&gt;bindparam(":password", $new_password);            
             $stmt-&gt;execute(); 
  
             return $stmt; 
         }
         catch(PDOException $e){
             echo $e-&gt;getMessage();
         }    
      }
  
      public function login($username,$email,$password){
         try{
            $stmt = $this-&gt;db-&gt;prepare("SELECT * FROM users WHERE username=:username OR email=:email LIMIT 1");
            $stmt-&gt;execute(array(':username'=&gt;$username, ':email'=&gt;$email));
            $userRow=$stmt-&gt;fetch(PDO::FETCH_ASSOC);
            if($stmt-&gt;rowCount() &gt; 0){
               if(password_verify($password, $userRow['password'])){
                  $_SESSION['session'] = $userRow['id'];
                  return true;
               }else{
                  return false;
               }
            }
         }
         catch(PDOException $e){
             echo $e-&gt;getMessage();
         }
     }
  
      public function is_loggedin(){
          if(isset($_SESSION['session'])){
              return true;
          }
      }
  
      public function redirect($url){
          header("Location:$url");
      }
  
      public function logout(){
          session_destroy();
          unset($_SESSION['session']);
          return true;
      }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>SQL Table:</strong></p>
  
  <pre><code>-- phpMyAdmin SQL Dump
  -- version 4.0.7
  -- http://www.phpmyadmin.net
  --
  -- Host: localhost
  -- Generation Time: Jun 19, 2015 at 11:52 AM
  -- Server version: 5.5.42
  -- PHP Version: 5.3.28
  
  SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
  SET time_zone = "+00:00";
  
  
  /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  /*!40101 SET NAMES utf8 */;
  
  --
  -- Database: `cl47-dbuser-1yz`
  --
  
  -- --------------------------------------------------------
  
  --
  -- Table structure for table `users`
  --
  
  CREATE TABLE IF NOT EXISTS `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(25) NOT NULL,
    `email` varchar(50) NOT NULL,
    `password` varchar(60) NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `username` (`username`),
    UNIQUE KEY `email` (`email`),
    UNIQUE KEY `id` (`id`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
  </code></pre>
  
  <p>Registering is fine, username, email &amp; password all insert.  Thanks for looking.</p>