Advertisement
Guest User

Untitled

a guest
Apr 1st, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. public function getUserByEmailAndPassword($e_mail, $password) {
  2.  
  3. $stmt = $this->conn->prepare("SELECT email, password, salt, name, surname, address FROM tbl_login WHERE email = ?");
  4.  
  5. $stmt->bind_param("s", $e_mail);
  6.  
  7. if ($stmt->execute()) {
  8.  
  9. $stmt->store_result();
  10. $stmt->bind_result($email, $password, $salt, $name, $surname, $address);
  11.  
  12. $user = array();
  13.  
  14. while($stmt->fetch()) {
  15. $tmp = array();
  16. $tmp["name"] = $name;
  17. $tmp["surname"] = $surname;
  18. $tmp["address"] = $address;
  19. $tmp["email"] = $email;
  20. $tmp["salt"] = $salt;
  21. $tmp["password"] = $password;
  22.  
  23. array_push($user, $tmp);
  24. }
  25.  
  26. $stmt->close();
  27.  
  28. return $user;
  29.  
  30. // verifying user password
  31. $salt = $user['salt'];
  32. $encrypted_password = $user['password'];
  33. $hash = $this->checkhashSSHA($salt, $password);
  34. // check for password equality
  35. if ($encrypted_password == $hash) {
  36. // user authentication details are correct
  37. return $user;
  38. }
  39. } else {
  40. return NULL;
  41. }
  42. }
  43.  
  44. $response = array("error" => FALSE);
  45.  
  46. if (isset($_POST['email']) && isset($_POST['password'])) {
  47.  
  48. // receiving the post params
  49. $email = $_POST['email'];
  50. $password = $_POST['password'];
  51.  
  52. // get the user by email and password
  53. $user = $db->getUserByEmailAndPassword($email, $password);
  54.  
  55. if ($user != false) {
  56. // user is found
  57. $response["error"] = FALSE;
  58. $response["user"]["name"] = $user["name"];
  59. $response["user"]["surname"] = $user["surname"];
  60. $response["user"]["address"] = $user["address"];
  61. $response["user"]["email"] = $user["email"];
  62. echo json_encode($response);
  63. } else {
  64. // user is not found with the credentials
  65. $response["error"] = TRUE;
  66. $response["error_msg"] = "Login credentials are wrong. Please try again!";
  67. $response["user"]["name"] = $user["name"];
  68. $response["user"]["surname"] = $user["surname"];
  69. $response["user"]["address"] = $user["address"];
  70. $response["user"]["email"] = $user["email"];
  71. echo json_encode($response);
  72. }
  73. } else {
  74. // required post params is missing
  75. $response["error"] = TRUE;
  76. $response["error_msg"] = "Required parameters email or password is missing!";
  77. echo json_encode($response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement