title: PHP UserName and Password Validation Check
body: <p>I'm creating a login page where the user name and password are entered and then checked against the database to see if they match (I have posted on this previously but my code was completely incorrect so I had to start over) Upon clicking the submit button the user should be directed to the homepage (index.php) if the two values match up or an error message should appear stating "Invalid login. Please try again." Very simple basic stuff. Yet, I cannot get any variation to work. </p>
  
  <p>Here is my code without the validation check. I believe this code is right but, if not, could someone please explain as to why. I am not asking anyone to write any code, just explain why it is not working properly.</p>
  
  <pre><code>&lt;?php
  
  function Password($UserName)
  {   
  
  //database login
  $dsn = 'mysql:host=XXX;dbname=XXX';
  $username='*****';
  $password='*****';
  //variable for errors
  $options = array(PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION);
  //try to run code
  try {
  //object to open database
  $db = new PDO($dsn,$username,$password, $options);
  //check username against password
      $SQL = $db-&gt;prepare("Select USER_PASSWORD FROM user WHERE USER_NAME = :USER_NAME");
      $SQL-&gt;bindValue(':USER_NAME', $UserName);
      $SQL-&gt;execute();
      $username = $SQL-&gt;fetch();
  
      if($username === false)
          {
              $Password = null;
          }
      else
          {
              $Password = $username['USER_PASSWORD'];
          }
  
      return $Password;
      $SQL-&gt;closeCursor();
      $db = null;
  
      } catch(PDOException $e){
          $error_message = $e-&gt;getMessage();
          echo("&lt;p&gt;Database Error: $error_message&lt;/p&gt;");
          exit();
      }
  ?&gt;
  </code></pre>
  
  <p>Now the validation code. I've googled this and found several hundred ways to do so but this method most closely matches my coding style. It is incomplete and I would like some help as to how to finish it properly and then where to place it within the code above. My assumption is right after this comment: "//check username against password". Now I've seen this version twice and in one version the check is for txtUserName and the other is just username. I believe there should be else statements after each if statement to direct them to the index.php page. Also, the third if statement is the check to see if the password matches the username. No variation of this did I understand. They were far too complex. </p>
  
  <pre><code>function Login()
  {     
  if(empty($_POST['txtUserName']))     
  {         
      $this-&gt;HandleError("UserName is empty!");         
      return false;     
  }          
  if(empty($_POST['txtPassword']))     
  {         
      $this-&gt;HandleError("Password is empty!");
              return false;     
  }           
  
  $username = trim($_POST['txtUserName']);
      $password = trim($_POST['txtPassword']);           
  
  if(!$this-&gt;($username,$password))     
  {         
      return false;     
  }           
  } 
  </code></pre>
  
  <p>I know I am asking a lot here. But I am very new to PHP and am really trying hard to learn it. And there is way too much info out there and most of it is not for beginners. Any, and all, help would be greatly appreciated. </p>