暫無描述

login.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. require_once('config.php');
  3. // Filter unsupported HTTP requests
  4. if ($_SERVER["REQUEST_METHOD"] !== "POST") {
  5. http_response_code(400); // use appropriate status
  6. echo json_encode(array("error" => "Endpoint only supports POST requests"));
  7. exit();
  8. }
  9. // Get input
  10. $json = file_get_contents('php://input');
  11. $data = json_decode($json);
  12. $email = isset($data->email) ? $data->email : '';
  13. $password = isset($data->password) ? $data->password : '';
  14. // Clean user input
  15. $email = $connection->real_escape_string($email);
  16. $password = $connection->real_escape_string($password);
  17. function Login($email, $password) {
  18. if ($email === '' || $password === '') {
  19. http_response_code(400); // use appropriate status
  20. echo json_encode(array("error" => "Fields 'email' and 'password' are required"));
  21. return;
  22. } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  23. http_response_code(400);
  24. echo json_encode(array("error" => "Invalid email '$email'"));
  25. return;
  26. }
  27. global $connection;
  28. $passwordHash = md5($password);
  29. $query = "SELECT *
  30. FROM UserData U
  31. WHERE U.email = '$email'
  32. AND U.password = '$passwordHash'";
  33. if ($result = $connection->query($query)) {
  34. if ($row = $result->fetch_object()) {
  35. $user = array(
  36. "id" => (int) $row->id,
  37. "username" => $row->username,
  38. "email" => $row->email,
  39. );
  40. http_response_code(200);
  41. echo json_encode($user, JSON_UNESCAPED_UNICODE);
  42. } else {
  43. http_response_code(400); // use appropriate status code
  44. echo json_encode(array("error" => "No user with given credentials"));
  45. }
  46. } else {
  47. http_response_code(500); // use appropriate status code
  48. echo json_encode(array("error" => $connection->error));
  49. }
  50. }
  51. // mail("vhernandezcastro@gmail.com", "test_login_android", json_encode($data));
  52. Login($email, $password);