How can i use preg_match and email validate in error handlers for user signup

Below is a copy of my code for signin error handlers, in the code my first else if and third else if statement loads back to the sigin.php page with corresponding error messages. please I would like to know if there are any errors in my statements that's causing these problems for me. the first statmenet is on line 21-24 (FILTER_VALIDATE_EMAIL AND !PREG_MATCH) and third statement is on line 29-31(!preg_match,$username).

<?php 
 if (isset($_POST['ssub'])) {
     
     require 'DB.php';  
     
     $username = $_POST['suser'];
     $email = $_POST['semail'];
     $password = $_POST['spwd'];
     $passwordRepeat = $_POST['spwd2'];
     $license = $_POST['slice'];
     $city = $_POST['scity'];
     $state = $_POST['sstate'];
     $zipcode = $_POST['scode'];
     $country = $_POST['scount'];
     $msg = $_POST['smsg'];
     
     if (empty($username) || empty($email) || empty($password) || empty($passwordRepeat) || empty($city) || empty($country)) {
         header("location:../signup.php?error=emptyfields&user".$username."&mail=".$email); 
         exit();
     } 
     else if (!filter_var ($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)) {
         header("location:../signup.php?error=inavalidmailuser");
         exit();
     }
     else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
         header("location:../signup.php?error=invalidmail&user".$username); 
         exit();
     } 
     else if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
         header("Location: ../signup.php?error=invalidmail&user=".$username);
     }
    
     else if ($password !== $passwordRepeat) {
         header("location:../signup.php?error=passwordcheck&user=".$username. "&mail=".$email); 
         exit();
     }
     else {
         $sql = "SELECT emailUsers FROM sign_logout WHERE emailUsers =?";
         $stmt = mysqli_stmt_init($conn);
         if (!mysqli_stmt_prepare($stmt, $sql)){
         header("location:../signup.php?error=sqlerror"); 
         exit(); 
         }
     
     else {
         mysqli_stmt_bind_param($stmt, "s", $email);
         mysqli_stmt_execute($stmt);
         mysqli_stmt_store_result($stmt);
         $resultcheck = mysqli_stmt_num_rows($stmt); 
         if ($resultcheck > 0) {
             header("location:../signup.php?error=usermailused"); 
             exit();
         }
         else {
              $sql = "INSERT INTO sign_logout (idUsername, emailUsers, pwdUsers, licenseUsers, cityUsers, stateUsers, zipUsers, countryUsers, msgUsers) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
             $stmt = mysqli_stmt_init($conn);
             if (!mysqli_stmt_prepare($stmt, $sql)){
             header("location:../signup.php?error=sqlerror"); 
              exit();  
             } 
             else {
                 $hashedpwd= password_hash($password, PASSWORD_DEFAULT);
                mysqli_stmt_bind_param($stmt, "sssssssss", $username, $email, $hashedpwd, $license, $city, $state, $zipcode, $country, $msg);
                mysqli_stmt_execute($stmt); 
                 header("location:../signup.php?=success"); 
              exit();   
             }
             
         }
     }
         
         
 } 
     mysqli_stmt_close($stmt);
     mysqli_close($conn); 
} 
else {
    header("location:../signup.php"); 
    exit();  
}


Read more here: https://stackoverflow.com/questions/66482264/how-can-i-use-preg-match-and-email-validate-in-error-handlers-for-user-signup

Content Attribution

This content was originally published by RCRACK at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: