Advertisement
Guest User

PHP loginsystem

a guest
Sep 21st, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.00 KB | None | 0 0
  1. <?php
  2. if (isset($_POST['submit'])) {
  3.     include_once 'dbh.php';
  4.     $first = mysqli_real_escape_string($conn, $_POST['first']);
  5.     $last = mysqli_real_escape_string($conn, $_POST['last']);
  6.     $email = mysqli_real_escape_string($conn, $_POST['email']);
  7.     $uid = mysqli_real_escape_string($conn, $_POST['uid']);
  8.     $pwd = mysqli_real_escape_string($conn, $_POST['pwd']);
  9.     //Error Handlers
  10.     //Check empty fields
  11.     if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
  12.         header("Location: ../signup.php?signup=empty");
  13.         exit();
  14.     } else {
  15.         //Check if input characters are valid
  16.         if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
  17.             header("Location: ../signup.php?signup=invalid");
  18.             exit();
  19.         } else {
  20.             //Check if email is valid
  21.             if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  22.                 header("Location: ../signup.php?signup=email");
  23.                 exit();
  24.             } else {
  25.                 $sql = "SELECT * FROM users WHERE user_uid='$uid'";
  26.                 $result = mysqli_query($conn, $sql);
  27.                 $resultCheck = mysqli_num_rows($result);
  28.                 if ($resultCheck > 0) {
  29.                     header("Location: ../signup.php?signup=usertaken");
  30.                     exit();
  31.                 } else {
  32.                     //Hashing the password
  33.                     $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
  34.                     //Insert the user into database
  35.                     $sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES
  36.                    ('$first', '$last', '$email', '$uid', $hashedPwd);";
  37.                     $result = mysqli_query($conn, $sql);
  38.                     header("Location: ../signup.php?signup=success");
  39.                     exit();
  40.                 }
  41.             }
  42.         }
  43.     }
  44. } else {
  45.     header("Location: ../signup.php");
  46.     exit();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement