No Description

login.php 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. header('Content-type: application/json');
  3. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  4. # Gets json from POST
  5. $json = json_decode(file_get_contents('php://input'), true);
  6. # Input validation for username y password
  7. $username = $json["username"];
  8. $password = $json["password"];
  9. if(strlen($username) == 0 or strlen($password) == 0){
  10. header("400 Bad Request", true, 400);
  11. exit();
  12. }
  13. include 'db.php';
  14. # Select username row
  15. $sql = "SELECT hash FROM login WHERE username = '".$username."'";
  16. $result = $conn->query($sql);
  17. if($result->num_rows){
  18. $result = $result->fetch_assoc();
  19. $hash = $result["hash"];
  20. } else {
  21. $conn->close();
  22. header("400 Bad Request", true, 400);
  23. exit();
  24. }
  25. # Verifies password
  26. if(!password_verify($password, $hash)){
  27. $conn->close();
  28. header("400 Bad Request", true, 400);
  29. exit();
  30. }
  31. # Returns 200
  32. $conn->close();
  33. header("200 OK", true, 200);
  34. exit();
  35. } else {
  36. header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405);
  37. exit();
  38. }
  39. ?>