Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. session_start();
  5.  
  6. //Initializing variables
  7.  
  8. $username ="";
  9. $emaill = "";
  10.  
  11. $errors = array();
  12.  
  13. //Connect to the Database
  14.  
  15. $db = mysqli_connect('localhost', 'USERNAME', 'PASSWORD', 'DATABASE') or die("Could not connect to database");
  16.  
  17. //Register Users
  18.  
  19. $username = mysqli_real_escape_string($db, $_POST['username']);
  20. $email = mysqli_real_escape_string($db, $_POST['email']);
  21. $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  22. $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
  23.  
  24. //Form Validation
  25.  
  26. if(empty($username)) {array_push($errors, "Username is required")};
  27. if(empty($email)) {array_push($errors, "Email is required")};
  28. if(empty($password_1)) {array_push($errors, "Password is required")};
  29. if($password_1 != $password_2){array_push($errors, "Passwords do not match")};
  30.  
  31. // Check Database for existing users with similar usernames
  32.  
  33. $user_check_query = "SELECT * FROM user WHERE username = '$username' or email = '$email' LIMIT 1";
  34.  
  35. $results = mysqli_query($db, $user_check_query);
  36. $user = mysqli_fetch_assoc($result);
  37.  
  38. if($user) {
  39.  
  40. if($user['username'] === $username){array_push($errors, "Username already exists");}
  41. if($user['email'] === $email){array_push($errors, "This email is already registered to a username");}
  42.  
  43. }
  44.  
  45. //register the userif no error
  46.  
  47. if(count($errors) == 0){
  48.  
  49. $password = md5($password_1); //this will encrpyt the password
  50. $query = "INSERT INTO user (username, email, password) VALUES ('$username', '$email', '$password')";
  51.  
  52. mysqli_query($db,$query);
  53. $_SESSION['username'] = $username;
  54. $_SESSION['success'] = "You are now logged in";
  55.  
  56. header('location: index.php');
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement