Advertisement
Guest User

Untitled

a guest
Jun 29th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // variable declaration
  5. $username = "";
  6. $email = "";
  7. $errors = array();
  8. $_SESSION['success'] = "";
  9.  
  10. // connect to database
  11. $db = mysqli_connect('localhost', 'root', '', 'registration');
  12.  
  13. // REGISTER USER
  14. if (isset($_POST['reg_user'])) {
  15. // receive all input values from the form
  16. $username = mysqli_real_escape_string($db, $_POST['username']);
  17. $email = mysqli_real_escape_string($db, $_POST['email']);
  18. $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  19. $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
  20.  
  21. // form validation: ensure that the form is correctly filled
  22. if (empty($username)) { array_push($errors, "Username is required"); }
  23. if (empty($email)) { array_push($errors, "Email is required"); }
  24. if (empty($password_1)) { array_push($errors, "Password is required"); }
  25.  
  26. if ($password_1 != $password_2) {
  27. array_push($errors, "The two passwords do not match");
  28. }
  29.  
  30. // register user if there are no errors in the form
  31. if (count($errors) == 0) {
  32. $password = md5($password_1);//encrypt the password before saving in the database
  33. $query = "INSERT INTO users (username, email, password)
  34. VALUES('$username', '$email', '$password')";
  35. mysqli_query($db, $query);
  36.  
  37. $_SESSION['username'] = $username;
  38. $_SESSION['success'] = "You are now logged in";
  39. header('location: index.php');
  40. }
  41.  
  42. }
  43.  
  44. // ...
  45.  
  46. // LOGIN USER
  47. if (isset($_POST['login_user'])) {
  48. $username = mysqli_real_escape_string($db, $_POST['username']);
  49. $password = mysqli_real_escape_string($db, $_POST['password']);
  50.  
  51. if (empty($username)) {
  52. array_push($errors, "Username is required");
  53. }
  54. if (empty($password)) {
  55. array_push($errors, "Password is required");
  56. }
  57.  
  58. if (count($errors) == 0) {
  59. $password = md5($password);
  60. $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
  61. $results = mysqli_query($db, $query);
  62.  
  63. if (mysqli_num_rows($results) == 1) {
  64. $_SESSION['username'] = $username;
  65. $_SESSION['success'] = "You are now logged in";
  66. header('location: index.php');
  67. }else {
  68. array_push($errors, "Wrong username/password combination");
  69. }
  70. }
  71. }
  72.  
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement