Advertisement
Guest User

Untitled

a guest
Feb 1st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. session_start();
  2.  
  3. // connect to database
  4. $db = mysqli_connect('localhost', 'root', '', 'multi_login');
  5.  
  6. // variable declaration
  7. $username = "";
  8. $email = "";
  9. $errors = array();
  10.  
  11. // call the register() function if register_btn is clicked
  12. if (isset($_POST['register_btn'])) {
  13. register();
  14. }
  15.  
  16. // REGISTER USER
  17. function register(){
  18. // call these variables with the global keyword to make them available in function
  19. global $db, $errors, $username, $email;
  20.  
  21. // receive all input values from the form. Call the e() function
  22. // defined below to escape form values
  23. $username = e($_POST['username']);
  24. $email = e($_POST['email']);
  25. $password_1 = e($_POST['password_1']);
  26. $password_2 = e($_POST['password_2']);
  27.  
  28. // form validation: ensure that the form is correctly filled
  29. if (empty($username)) {
  30. array_push($errors, "Username is required");
  31. }
  32. if (empty($email)) {
  33. array_push($errors, "Email is required");
  34. }
  35. if (empty($password_1)) {
  36. array_push($errors, "Password is required");
  37. }
  38. if ($password_1 != $password_2) {
  39. array_push($errors, "The two passwords do not match");
  40. }
  41.  
  42. // register user if there are no errors in the form
  43. if (count($errors) == 0) {
  44. $password = md5($password_1);//encrypt the password before saving in the database
  45.  
  46. if (isset($_POST['user_type'])) {
  47. $user_type = e($_POST['user_type']);
  48. $query = "INSERT INTO users (username, email, user_type, password)
  49. VALUES('$username', '$email', '$user_type', '$password')";
  50. mysqli_query($db, $query);
  51. $_SESSION['success'] = "New user successfully created!!";
  52. header('location: home.php');
  53. }else{
  54. $query = "INSERT INTO users (username, email, user_type, password)
  55. VALUES('$username', '$email', 'user', '$password')";
  56. mysqli_query($db, $query);
  57.  
  58. // get id of the created user
  59. $logged_in_user_id = mysqli_insert_id($db);
  60.  
  61. $_SESSION['user'] = getUserById($logged_in_user_id); // put logged in user in session
  62. $_SESSION['success'] = "You are now logged in";
  63. header('location: index.php');
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement