title: PDO Register and Login Password Matching
body: <p>Okay.. so to start off I only have Php 5.3 so I can't use bcrypt, I am not familiar with salt but an completely willing to do it if someone can help me out. I also would like to know if this script is bad or good for sql injections. My biggest problems is when I use something like crypt and try to get my passwords to match, it won't. I've been working on this for days and can't seem to find the right solution to my problem. the code is not done yet, but its able to run. I'm just doing this on wamp so i dunno if thats a problem? but i cant imagine it is.</p>
  
  <p>REGISTER . PHP
              
  
  <pre><code>            if ((strlen($username)) &lt; 6 || (preg_match("/[^\w-.]/", $username)) ) {
                  header('Location: Register.php?fail=1');
                  die();
              }
              if ((strlen($password)) &lt; 8) {
                  header('Location: Register.php?fail=2');
                  die();
              }
              if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                  header('Location: Register.php?fail=3');
                  die();
              }
  
              /*
              TRIED METHODS
          $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
          $salt = base64_encode($salt);
          $salt = str_replace('+', '.', $salt);
          $hash = crypt('rasmuslerdorf', '$2y$10$'.$salt.'$');
  
          $password = $hash;
          echo "&lt;script&gt;alert('$password');&lt;/script&gt;";
  
  
  
              $salt = '$2a$07$usesomadasdsadsadsadasdasdasdsadesillystringfors';
              $digest = crypt($password, $salt);
  
              if (crypt($password, $digest) == $digest){
                      echo "&lt;script&gt;alert('logged in');&lt;/script&gt;";
              }else{
                  header('Location: Login.php?fail=3');
                  die();
              }
  
  
          */
  
          //PDO CONNECTION
          function pdo_connect() {
              try {
                  $db = new PDO("mysql:host=localhost;dbname=XXX", "XXX", "XXX");
                  return $db;
              } catch (PDOException $e) {
                  //echo $e-&gt;getMessage();
                  //return false;
                  header('Location: Register.php?fail=6');
              }
          }
  
          //CHECK IF USERNAME EXISTS
          function usernameCheck($username) {
              $con = pdo_connect();
              $ustmt = $con-&gt;prepare("SELECT u_users FROM users WHERE u_users = :name");
              $ustmt-&gt;bindParam(':name', $username);
              $ustmt-&gt;execute();
              if($ustmt-&gt;rowCount() &gt; 0){
                  header('Location: Register.php?fail=4');
                  die();
              } 
              $con = null;    
          }
          echo usernameCheck($username);
  
          //CHECK IF EMAIL EXISTS
          function emailCheck($email) {
              $con = pdo_connect();
              $estmt = $con-&gt;prepare("SELECT u_email FROM users WHERE u_email = :name");
              $estmt-&gt;bindParam(':name', $email);
              $estmt-&gt;execute();
  
              if($estmt-&gt;rowCount() &gt; 0){
                  header('Location: Register.php?fail=5');
                  die();
              } 
              $con = null;
          }
          echo emailCheck($email);
  
          //INSERT EMAIL TO NEWSLETTER
          function emailnewsletterCheck($email) {
              $con = pdo_connect();
              $nstmt = $con-&gt;prepare("SELECT n_email FROM newsletter WHERE n_email = :email");
              $nstmt-&gt;bindParam(':email', $email);
              $nstmt-&gt;execute();
  
              if($nstmt-&gt;rowCount() &lt; 1){
                  $addstmt = $con-&gt;prepare('INSERT INTO newsletter (n_email) VALUES (:email)');
                  $addstmt-&gt;bindParam(':email', $email);
                  $addstmt-&gt;execute();
              } 
              $con = null;
          }
          echo emailnewsletterCheck($email);
  
          //INSERT
          function insert($username,$password,$email,$type) {  
              $con = pdo_connect();
              $password = md5($password);
              $istmt = $con-&gt;prepare('INSERT INTO users (u_users, u_private, u_email, u_type) VALUES (:username, :password, :email, :type)');
              $istmt-&gt;execute(array(
                  ':username' =&gt; $username,
                  ':password' =&gt; $password,
                  ':email' =&gt; $email,
                  ':type' =&gt; $type
                  ));
              $con = null;
              header('Location: Login.php?success=1');
          }   
          echo insert($username,$password,$email,$type);
  
          }//end submit               
          ?&gt;
  
          &lt;?php
          $page_title = "NS : Web Development : Register";
          $page_desc = "Register with us for great deals on website development.";
          $services = 0;
          include_once 'header.php';
          ?&gt;
  
          &lt;script type="text/javascript"&gt;
          // This function checks if the username field is at least 6 characters long.
          function checkUsernameForLength(whatYouTyped) {
              var fieldset = whatYouTyped.parentNode.parentNode.parentNode;
              var txt = whatYouTyped.value;
              if (txt.length &gt; 5) {
                  $("span.hint").hide();
              }
          }
          // If the password is at least 4 characters long
          function checkPassword(whatYouTyped) {
              var fieldset = whatYouTyped.parentNode.parentNode.parentNode;
              var txt = whatYouTyped.value;
              if (txt.length &gt; 7) {
                  $("span.hint").hide();
              }
          }
          // This function checks the email address blah@blah.blah
          function checkEmail(whatYouTyped) {
              var fieldset = whatYouTyped.parentNode.parentNode.parentNode;
              var txt = whatYouTyped.value;
              if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
                  $("span.hint").hide();
              } 
          }
          // this part is for the form field hints to display
          // only on the condition that the text input has focus.otherwise, it stays hidden.
          function addLoadEvent(func) {
            var oldonload = window.onload;
            if (typeof window.onload != 'function') {
              window.onload = func;
            } else {
              window.onload = function() {
                oldonload();
                func();
              }
            }
          }
          function prepareInputsForHints() {
            var inputs = document.getElementsByTagName("input");
            for (var i=0; i&lt;inputs.length; i++){
              inputs[i].onfocus = function () {
                this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
              }
              inputs[i].onblur = function () {
                this.parentNode.getElementsByTagName("span")[0].style.display = "none";
              }
            }
          }
          addLoadEvent(prepareInputsForHints);
          &lt;/script&gt;
  
  
          &lt;div class="jumbotron"&gt;
          &lt;div class="container"&gt;
  
          &lt;h1&gt;Register for &lt;font color="fb1576"&gt;great&lt;/font&gt; opportunities&lt;/h1&gt;
          &lt;p&gt;Get full quotes, package &lt;font color="fb1576"&gt;deals&lt;/font&gt;, news and updates on the latest themes and scripts, and even &lt;font color="fb1576"&gt;win&lt;/font&gt; free prizes&lt;font color="fb1576"&gt;!&lt;/font&gt;
  
          &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="container"&gt;
  
          &lt;!-- row of columns --&gt;
          &lt;div class="row"&gt;
  
          &lt;?php
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 1 ){
              echo "&lt;div class='alert alert-danger'&gt;Username must be at least 6 characters in length and can only contain characters matching (a-z) (A-Z) (0-9) and '_' Please try again. &lt;a href='Register.php'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; Close&lt;/a&gt;&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 2 ){
              echo "&lt;div class='alert alert-danger'&gt;Password must be at least 8 characters in length and cannot exceed 25.  Please try again. &lt;a href='Register.php'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; Close&lt;/a&gt;&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 3 ){
              echo "&lt;div class='alert alert-danger'&gt;E-mail is not valid. Please try again. &lt;a href='Register.php'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; Close&lt;/a&gt;&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 4 ){
              echo "&lt;div class='alert alert-danger'&gt;Username you chose already exists. Please try again. &lt;a href='Register.php'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; Close&lt;/a&gt;&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 5 ){
              echo "&lt;div class='alert alert-danger'&gt;E-mail you entered is already in use. Please try again. &lt;a href='Register.php'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; Close&lt;/a&gt;&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 6 ){
              echo "&lt;div class='alert alert-danger'&gt;Something went wrong, we couldn't submit your registration. Please try again later. &lt;a href='Register.php'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; Close&lt;/a&gt;&lt;/div&gt;";
          }
          ?&gt;
  
          &lt;form name="basicform" id="basicform" method="POST"&gt;
  
          &lt;fieldset&gt;
          &lt;div class="input-group"&gt;
              &lt;label for="username"&gt;Choose a Username:&lt;/label&gt;&lt;br&gt;
              &lt;input type="text" id="username" name="username" onkeyup="checkUsernameForLength(this);" required class="form-control" maxlength="25" pattern=".{6,}"/&gt;
              &lt;span class="hint"&gt;Usernames must be a least 6 characters in length and cannot exceed 25. Characters must match (a-z) (A-Z) (0-9) and '_'&lt;/span&gt;
          &lt;/div&gt;
          &lt;/fieldset&gt;
  
          &lt;fieldset&gt;
          &lt;div class="input-group"&gt;
              &lt;label for="password"&gt;Enter a password:&lt;/label&gt;&lt;br&gt;
              &lt;input type="password" id="password" name="password" onkeyup="checkPassword(this);" required class="form-control" maxlength="25" pattern=".{7,}"/&gt;
              &lt;span class="hint"&gt;The password can be any combination of &lt;strong&gt;characters&lt;/strong&gt;, and must be at least 8 characters in length and cannot exceed 25.&lt;/span&gt;
          &lt;/div&gt;
          &lt;/fieldset&gt;
  
          &lt;fieldset&gt;
          &lt;div class="input-group"&gt;
              &lt;label for="email"&gt;Enter your email address:&lt;/label&gt;&lt;br&gt;
              &lt;input type="text" id="email" name="email" onkeyup="checkEmail(this);" required class="form-control" maxlength="30" /&gt;
              &lt;span class="hint"&gt;Please enter your real email address (ie: you@emailprovider.com)&lt;/span&gt;
          &lt;/div&gt;
          &lt;/fieldset&gt;
  
          &lt;fieldset&gt;
          &lt;label for="type"&gt;Pick your position of registration:&lt;/label&gt;&lt;br&gt;
          &lt;select name="type"&gt;
          &lt;option name="type" value="Client"&gt;I am a client looking for work to be done&lt;/option&gt;
          &lt;option name="type" value="Employer"&gt;I am an employer looking for a potential hire&lt;/option&gt;
          &lt;option name="type" value="Employee"&gt;I am an employee looking to be hired&lt;/option&gt;
          &lt;/select&gt;
          &lt;/fieldset&gt;
  
          &lt;fieldset&gt;
          &lt;button type="submit" class="btn btn-primary" name="submit" value="submit"&gt;Register Now&lt;/button&gt;
          &lt;/fieldset&gt;
  
          &lt;/form&gt;
  
          &lt;/div&gt;
          &lt;!-- //row of columns --&gt;
          &lt;?php
          include_once 'footer.php';
          ?&gt;
  </code></pre>
  
  <p>LOGIN . PHP
              
  
  <pre><code>            $username = $_POST['username'];
              $password = $_POST['password'];
  
              //before we even bother connecting to the db start validating
              if ( (empty($username)) || (empty($password)) ) {
                  header('Location: Login.php?fail=1');
                  die();
              }
              if ( ((strlen($username)) &gt;25) || ((strlen($password)) &gt;25) ) {
                  header('Location: Login.php?fail=2');
                  die();
              }
              if ( (preg_match("/[^\w-.]/", $username)) ) {
                  header('Location: Login.php?fail=3');
                  die();
              }
  
              /*
  
              TRIED METHODS
          $salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);
          $salt = base64_encode($salt);
          $salt = str_replace('+', '.', $salt);
          $hash = crypt('rasmuslerdorf', '$2y$10$'.$salt.'$');
  
          $password = $hash;
          echo "&lt;script&gt;alert('$password');&lt;/script&gt;";
  
  
  
              $salt = '$2a$07$usesomadasdsadsadsadasdasdasdsadesillystringfors';
              $digest = crypt($password, $salt);
  
              if (crypt($password, $digest) == $digest){
                      echo "&lt;script&gt;alert('logged in');&lt;/script&gt;";
              }else{
                  header('Location: Login.php?fail=3');
                  die();
              }
  
  
          */
  
  
          //PDO CONNECTION
          function pdo_connect() {
              try {
                  $db = new PDO("mysql:host=localhost;dbname=XXX", "XXX", "XXX");
                  return $db;
              } catch (PDOException $e) {
                  //echo $e-&gt;getMessage();
                  //return false;
                  header('Location: Register.php?fail=6');
              }
          }
  
          //CHECK IF USERNAME EXISTS
          function checkLogin($username,$password) {
              $con = pdo_connect();
                  //$getlogin = $con-&gt;query
                  $getlogin = $con-&gt;prepare("SELECT u_users,u_private FROM users WHERE u_users = :username AND u_private = :password");
                  $getlogin-&gt;bindValue(':username', $username, PDO::PARAM_STR);
                  $getlogin-&gt;bindValue(':password', $password, PDO::PARAM_STR);
                  $getlogin-&gt;execute();
  
              if($getlogin-&gt;rowCount() &gt; 0){
              echo "&lt;script&gt;alert('yes');&lt;/script&gt;";
              } 
              $con = null;    
          }
          echo checkLogin($username,$password);
          echo "&lt;script&gt;alert('success');&lt;/script&gt;";
          }
  
  
          ?&gt;
  
          &lt;?php
          $page_title = "NS : Web Development : Register";
          $page_desc = "Register with us for great deals on website development.";
          $services = 0;
          include_once 'header.php';
          ?&gt;
  
          &lt;div class="jumbotron"&gt;
          &lt;div class="container"&gt;
  
          &lt;h1&gt;&lt;font color="fb1576"&gt;Members&lt;/font&gt; log in&lt;/h1&gt;
          &lt;p&gt; Not yet a member? &lt;a href="Register.php"&gt;&lt;font color="fb1576"&gt;Sign up today!&lt;/font&gt;&lt;/a&gt;
  
          &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="container"&gt;
  
  
          &lt;?php
          if ( isset($_GET['success']) &amp;&amp; $_GET['success'] == 1 ){
              echo "&lt;div class='alert alert-success'&gt;Registration successful. Please log in.&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 1 ){
              echo "&lt;div class='alert alert-danger'&gt;Username or Password cannot be left blank.&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 2 ){
              echo "&lt;div class='alert alert-danger'&gt;Sorry, this is not a valid Username or Password.&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 3 ){
              echo "&lt;div class='alert alert-danger'&gt;Username or Password incorrect, please try again.&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 5 ){
              echo "&lt;div class='alert alert-danger'&gt;E-mail you entered is already in use. Please try again. &lt;a href='Register.php'&gt;&lt;span class='glyphicon glyphicon-remove'&gt;&lt;/span&gt; Close&lt;/a&gt;&lt;/div&gt;";
          }
          if ( isset($_GET['fail']) &amp;&amp; $_GET['fail'] == 6 ){
              echo "&lt;div class='alert alert-danger'&gt;Something went wrong. Please try again later. &lt;/div&gt;";
          }
          ?&gt;
  
          &lt;form class="form-signin" role="form" method="POST"&gt;
          &lt;h2 class="form-signin-heading"&gt;Please sign in&lt;/h2&gt;
  
          &lt;p&gt;
          &lt;input type="text" class="form-control" placeholder="Username" name="username" required autofocus&gt;
          &lt;/p&gt;
          &lt;br&gt;
          &lt;p&gt;
          &lt;input type="password" class="form-control" placeholder="Password" name="password" required&gt;
          &lt;/p&gt;
  
          &lt;label class="checkbox"&gt;
          &lt;input type="checkbox" value="remember-me"&gt; Remember me
          &lt;/label&gt;
  
          &lt;button class="btn btn-lg btn-primary btn-block" type="submit" name="submit" value="submit"&gt;Sign in&lt;/button&gt;
  
          &lt;/form&gt;
  
  
  
          &lt;?php
          include_once 'footer.php';
          ?&gt;
  </code></pre>
  
  <p>I REALLLLLYY need to get it so it is secure for my server on launch and can login a user safely.</p>