Advertisement
Skorpius

Variety of Functions

Feb 6th, 2023 (edited)
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.82 KB | None | 0 0
  1. //Variety of functions for signup and login system
  2.  
  3. # Checking if any of the fields have been left blank
  4.    function emptyInputSignup($fname, $lname, $uname, $email, $pwd, $rpwd){
  5.         $result;
  6.         if(empty($fname) || empty($lname) || empty($uname) || empty($email) || empty($pwd) || empty($rpwd)){
  7.             $result = true;
  8.         }else{
  9.             $result = false;
  10.         }
  11.         return $result;
  12.     }
  13.  
  14. /************************************************************************************************************************************************************************************/
  15.  
  16. # Checking if any of the fields have been left blank
  17.    function invalidUid($uname){
  18.         $result;
  19.         if(!preg_match("/^[a-zA-Z0-9]*$/", $uname)){
  20.             // True = error
  21.             $result = true;
  22.         }else{
  23.             $result = false;
  24.         }
  25.         return $result;
  26.     }
  27.  
  28. /************************************************************************************************************************************************************************************/
  29.  
  30. # Checking if valid email string
  31.    function invalidEmail($email){
  32.         $result;
  33.         if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
  34.             //true = error
  35.             $result = true;
  36.         }else{
  37.             $result = false;
  38.         }
  39.         return $result;
  40.     }
  41.  
  42. /************************************************************************************************************************************************************************************/
  43.  
  44. # Checking if passwords match
  45.    function pwdMatch($pwd, $cpwd){
  46.         $result;
  47.         if($pwd != $cpwd){
  48.             // true = error
  49.             $result = true;
  50.         }else{
  51.             $result = false;
  52.         }
  53.         return $result;
  54.     }
  55.  
  56. /************************************************************************************************************************************************************************************/
  57.  
  58.     # Password not long enough
  59.        function pwdTooShort($pwd){
  60.             $result;
  61.             if($pwd >= 8){
  62.                 // true = error
  63.                 $result = true;
  64.             }else{
  65.                 $result = false;
  66.             }
  67.             return $result;
  68.         }
  69.  
  70. /************************************************************************************************************************************************************************************/
  71.  
  72. # Checking if username already exists
  73.    function uidExists($dbc, $uname, $email){
  74.         $sql = "SELECT * FROM adminusers WHERE username = ? OR email = ?;";
  75.        
  76.        
  77.        
  78.         $stmt = mysqli_stmt_init($dbc);
  79.        
  80.         if(!mysqli_stmt_prepare($stmt, $sql)){
  81.             header('Location: register.php?error=stmtfailed');
  82.             exit();
  83.         }
  84.        
  85.         mysqli_stmt_bind_param($stmt, "ss", $uname, $email);
  86.         mysqli_stmt_execute($stmt);
  87.        
  88.         $resultData = mysqli_stmt_get_result($stmt);
  89.        
  90.         if($row = mysqli_fetch_assoc($resultData)){
  91.             return $row;
  92.         }else{
  93.             $result = false;
  94.             return $result;
  95.         }
  96.        
  97.         mysqli_stmt_close($stmt);
  98.        
  99.     }
  100.  
  101. /************************************************************************************************************************************************************************************/
  102.  
  103.  # Checking if username already exists
  104.    function createUser($dbc, $fname, $lname, $uname, $email, $pwd){
  105.         $sql = "INSERT INTO adminusers (first, last, email, username, password) VALUES (?,?,?,?,?);";
  106.        
  107.         $stmt = mysqli_stmt_init($dbc);
  108.        
  109.         if(!mysqli_stmt_prepare($stmt, $sql)){
  110.             header('Location: register.php?error=databaseerror');
  111.             exit();
  112.         }
  113.        
  114.         //Hashing pwd
  115.         $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
  116.        
  117.        
  118.         mysqli_stmt_bind_param($stmt, "sssss", $fname, $lname, $email, $uname, $hashedPwd);
  119.         mysqli_stmt_execute($stmt);
  120.        
  121.         mysqli_stmt_close($stmt);
  122.         header("Location: register.php?none");
  123.         exit();
  124.     }
  125.  
  126. /************************************************************************************************************************************************************************************/
  127.  
  128. function emptyInputLogin($uname, $pwd){
  129.         $result;
  130.         if(empty($uname) || empty($pwd)){
  131.             $result = true;
  132.         }else{
  133.             $result = false;
  134.         }
  135.         return $result;
  136.     }
  137.  
  138. /************************************************************************************************************************************************************************************/
  139.  
  140. function loginUser($dbc, $uname, $pwd){
  141.        
  142.         $uidExists = uidExists($dbc, $uname, $uname);
  143.        
  144.         //If false username/email doesn't exist in dbase
  145.         if($uidExists === false){
  146.             header('Location: login.php?error=incorrectlogindetails');
  147.             exit();
  148.         }
  149.        
  150.         $pwd_hashed = $uidExists['password'];
  151.         $checkpwd = password_verify($pwd, $pwd_hashed);
  152.        
  153.         if($checkpwd === false){
  154.             header('Location: login.php?error=incorrectlogindetails');
  155.             exit();
  156.         }elseif($checkpwd === true){
  157.             $_SESSION['userid'] = $uidExists['id'];
  158.             $_SESSION['useruid'] = $uidExists['username'];
  159.             $_SESSION['useremail'] = $uidExists['email'];
  160.             $_SESSION['userfirst'] = $uidExists['first'];
  161.             $_SESSION['userlast'] = $uidExists['last'];
  162.             $_SESSION['userstatus'] = $uidExists['status'];
  163.            
  164.            
  165.            
  166.             header('Location: index.php');
  167.             exit();
  168.         }
  169.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement