how to create login and register account with validation using preg_match

        **//login.php
        <?php
            include('db/connection.php');
            error_reporting(0);

            session_start();

            if(isset($_POST['submit'])){

              $username = $_POST['username'];
              $password = $_POST['password'];

              $username = str_replace(' ', '', $username);

               if ($username == null || empty($password)) 
               {
                  $userCheck = " *Username or Password field is required";
                  $errorCheck = 1;
               } 

               else if (!preg_match('/^[a-z0-9_]{5,12}$/', $username)) {
                  $userCheck = " *Username must have at least 5-12 characters";
                  $errorCheck = 1;
               }
               else
               {
                  $errorCheck = 0;
               }

               $password = md5($password);

              if($errorCheck == 0)
              {
                  $query = sprintf("SELECT * from users WHERE username = '%s' and password = '%s' and status='1' LIMIT 1", 
                      mysql_real_escape_string(stripslashes($_POST['username'])), 
                      mysql_real_escape_string(stripslashes(md5($_POST['password'])))  
                      );                  
                  $result = mysqli_query($con,$query);
                  $row = mysqli_fetch_array($result);

                  if(count($row) > 0)
                      {
                          $_SESSION['username']=$row['username'];
                          header("Location:customer/home");
                      }
                      else
                      {
                          $errorCheck = 1;
                          $userCheck = "Invalid Username or Password.";   
                      }
              }`enter code here`
            }
            ?>


//register.php
 <?php
                      if(isset($_POST['register'])){
                          $firstname = mysql_real_escape_string(stripslashes($_POST['firstname']));
                          $lastname = mysql_real_escape_string(stripslashes($_POST['lastname']));
                          $username = mysql_real_escape_string(stripslashes($_POST['username']));
                          $password = mysql_real_escape_string(stripslashes($_POST['password']));
                          $con=mysqli_connect("localhost","root","","cafe_uk");


                          $fnameCheck;
                          $lnameCheck;
                          $userCheck;
                          $passwordCheck;
                          $already;

                          $errorCheck = false;


                          // Username Already exist
                    $query = mysql_query("SELECT * FROM users WHERE username='$username'");
                    if(mysql_num_rows($query)>0){
                        $already = "*User already exist";
                        $errorCheck = 1;
                    }

                                      // Validation for Firstname
                      if ($firstname == null) {
                          $fnameCheck = "*First Name field is required";
                          $errorCheck = 1;
                      }if (!ctype_alpha(str_replace(' ', '', $firstname)) && $firstname != null) {
                          $fnameCheck = " Invalid Firstname";
                          $errorCheck = 1;
                      }


                                      // Validation for Lastname
                      if ($lastname == null) {
                          $lnameCheck = " *Last Name field is required";
                          $errorCheck = 1;
                      }if (!ctype_alpha(str_replace(' ', '', $lastname)) && $lastname != null) {
                          $lnameCheck = " Invalid Lastname";
                          $errorCheck = 1;
                      }

                                      // Validation for Username
                      if ($username == null) {
                          $userCheck = " *Username field is required";
                          $errorCheck = 1;
                      }if (!ctype_alpha(str_replace(' ', '', $username)) && $username!= null) {
                          $userCheck= " Invalid Username";
                          $errorCheck = 1;
                      }if (!preg_match('/^[a-z0-9_]{6,12}$/', $username) && $username != null) {
                          $userCheck = " *Username must have at least 6-12 characters";
                          $errorCheck = 1;
                      }


                                        // Validation for Password
                      if (empty($password)) {
                          $passwordCheck = " *Password field is required";
                          $errorCheck = 1;
                      }if (!preg_match('#.*^(?=.{8,12})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#',

$password) && $password != null) {
$passwordCheck = ” *Password must contain 1 upper case,1 lower case,1 digit and at least 8-12
characters”;
$errorCheck = 1;
}

                      $password = md5($password);
                                  //CORRECT!
                        if($errorCheck == false){
                                $query = "INSERT INTO users (firstname,lastname,username,password,status) VALUES ('$firstname',

‘$lastname’, ‘$username’, md5(‘$password’), ‘1’)”;
$result = mysqli_query($con,$query);

                                echo '<script type="text/javascript">';
                                echo 'alert("Successfully registered!");';
                                echo '</script>';
                            }
} 
?>
            </form>


              <?php
            if(isset($errorCheck)){
                                                    // echo' <a href=manage></a>';
                                                    echo "</br>";
                                                    echo' <div class="panel-footer" style="text-align:center;">';

                                                        ?> 
                                                        <?php
                                                        if (isset($fnameCheck)) {
                                                            echo "<font style="color: red;">$fnameCheck</font>";
                                                            echo '</br>';
                                                        }
                                                        ?>  
                                                        <?php
                                                        if (isset($lnameCheck)) {
                                                            echo "<font style="color: red;">$lnameCheck</font>";
                                                            echo '</br>';
                                                        }
                                                        ?>
                                                        <?php
                                                        if (isset($userCheck)) {
                                                            echo "<font style="color: red;">$userCheck</font>";
                                                            echo '</br>';
                                                        }
                                                        ?> 


                                                        <?php
                                                        if (isset($already)) {
                                                            echo "<font style="color: red;">$already</font>";
                                                             echo '</br>';

                                                        }
                                                        ?>

                                                         <?php
                                                        if (isset($passwordCheck)) {
                                                            echo "<font style="color: red;">$passwordCheck</font>";
                                                             echo '</br>';

                                                        }
                                                        ?>  

                                                          <?php

                  }
                ?>**


Source: mysql

Leave a Reply