Advertisement
Guest User

log.php

a guest
Aug 25th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. <?php
  2.  
  3. session_start();
  4.  
  5. if (isset($_POST['submit'])) {
  6.  
  7. include '../db.php';
  8.  
  9. $username = mysqli_real_escape_string($conn, $_POST['username']);
  10. $password = mysqli_real_escape_string($conn, $_POST['password']);
  11.  
  12. //Error handlers
  13. //Check if inputs are empty
  14. if (empty($username) || empty($password)) {
  15. header("Location: ../login.php?login=empty");
  16. exit();
  17. } else {
  18. $sql = "SELECT * FROM users WHERE username='$username'";
  19. $result = mysqli_query($conn, $sql);
  20. $resultCheck = mysqli_num_rows($result);
  21. if ($resultCheck < 1) {
  22. header("Location: ../login.php?login=error");
  23. exit();
  24. } else {
  25. if ($row = mysqli_fetch_assoc($result)) {
  26. //De-hashing the password
  27. $hashedPwdCheck = password_verify($password, $row['password']);
  28. if ($hashedPwdCheck == false) {
  29. header("Location: ../login.php?login=password-error");
  30. exit();
  31. } elseif ($hashedPwdCheck == true) {
  32. //Log in the user here
  33. $_SESSION ['id'] = $row['id'];
  34. $_SESSION ['username'] = $row['username'];
  35. $_SESSION ['email'] = $row['email'];
  36. $_SESSION ['password'] = $row['password'];
  37. header("Location: ../index.php?login=success");
  38. exit();
  39. }
  40. }
  41. }
  42. }
  43. } else {
  44. header("Location: ../login.php?login=error");
  45. exit();
  46. }
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement