Advertisement
Guest User

Untitled

a guest
Apr 5th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. REGISTER FUNCTION
  2. <?php
  3. $username = "";
  4. $email = "";
  5. $errors = array();
  6.  
  7. // connect to the database
  8. $db = mysqli_connect('localhost', 's1806392_admin', 'kJbjej4Y7jxxt3H', 's1806392_logins');
  9.  
  10. // REGISTER USER
  11. if (isset($_POST['submit'])) {
  12. // receive all input values from the form
  13. $username = mysqli_real_escape_string($db, $_POST['username']);
  14. $email = mysqli_real_escape_string($db, $_POST['email']);
  15. $password_1 = mysqli_real_escape_string($db, $_POST['password1']);
  16. $password_2 = mysqli_real_escape_string($db, $_POST['password2']);
  17.  
  18. // form validation: ensure that the form is correctly filled ...
  19. // by adding (array_push()) corresponding error unto $errors array
  20. if (empty($username)) { array_push($errors, "Username is required"); }
  21. if (empty($email)) { array_push($errors, "Email is required"); }
  22. if (empty($password_1)) { array_push($errors, "Password is required"); }
  23.  
  24. if ($password_1 != $password_2) {
  25. array_push($errors, "The two passwords do not match");
  26. }
  27.  
  28. // first check the database to make sure
  29. // a user does not already exist with the same username and/or email
  30. $user_check_query = "SELECT * FROM shop_users WHERE username='$username' OR email='$email' LIMIT 1";
  31. $result = mysqli_query($db, $user_check_query);
  32. $user = mysqli_fetch_assoc($result);
  33.  
  34. if ($user) { // if user exists
  35. if ($user['username'] === $username) {
  36. array_push($errors, "Username already exists");
  37. }
  38.  
  39. if ($user['email'] === $email) {
  40. array_push($errors, "email already exists");
  41. }
  42. }
  43.  
  44. // Finally, register user if there are no errors in the form
  45. if (count($errors) == 0) {
  46. $password = md5($password_1);//encrypt the password before saving in the database
  47.  
  48. $query = "INSERT INTO shop_users (username, email, password)
  49. VALUES('$username', '$email', '$password')";
  50. mysqli_query($db, $query);
  51. $_SESSION['username'] = $username;
  52. $_SESSION['success'] = "You are now logged in";
  53. header('location: index.php');
  54. }
  55. }
  56. ?>
  57. REGISTER PAGE
  58. <?php require_once ("config.php");?>
  59. <?php include("header.php");?>
  60.  
  61.  
  62. <!-- Page Content -->
  63. <div class="container">
  64.  
  65. <body background="https://cdn.discordapp.com/attachments/439349003805786112/531872173183336459/world.png">
  66.  
  67. <header>
  68. <h1 class="text-center" style="color:white;">Register</h1>
  69. <h2 class="text-center"><?php display_message(); ?></h2>
  70. <div class="col-sm-4 col-sm-offset-5">
  71. <!--<form class="" action="" method="post" action="login_user" enctype="multipart/form-data">-->
  72. <form class="" action="" method="post" enctype="multipart/form-data">
  73. <div class="form-group"><label for="" style="color:white;">
  74. Username<input type="text" name="username" class="form-control"></label>
  75. </div>
  76. <div class="form-group"><label for="" style="color:white;">
  77. Email<input type="text" name="email" class="form-control"></label>
  78. </div>
  79. <div class="form-group"><label for="password" style="color:white;">
  80. Password<input type="password" name="password1" class="form-control"></label>
  81. </div>
  82. <div class="form-group"><label for="password" style="color:white;">
  83. Confirm Password<input type="password" name="password2" class="form-control"></label>
  84. </div>
  85.  
  86. <div class="form-group">
  87. <input type="submit" name="submit" class="btn btn-primary" >
  88. <?php
  89. include 'functions.php';
  90. echo register_user();?>
  91. </div>
  92. </form>
  93. </div>
  94.  
  95.  
  96. </header>
  97.  
  98.  
  99. </div>
  100.  
  101. </div>
  102. </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement