Nessuna descrizione

signup.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. # Hashed password
  14. $hash = password_hash($password, PASSWORD_BCRYPT);
  15. include 'db.php';
  16. # Starts a transaction
  17. $conn->autocommit(FALSE);
  18. $conn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
  19. # Inserts into login
  20. $sql = "INSERT INTO login (username, hash) VALUES ('".$username."', '".$hash."')";
  21. if($conn->query($sql) === FALSE){
  22. $conn->rollback();
  23. $conn->close();
  24. header("500 Internal Server Error", true, 500);
  25. exit();
  26. }
  27. # Grabs all data
  28. $userid = $conn->insert_id;
  29. $nombre = $json["nombre"];
  30. $organizacion = $json["organizacion"];
  31. $puesto = $json["puesto"];
  32. $urbanizacion = $json["urbanizacion"];
  33. $calle = $json["calle"];
  34. $pueblo = $json["pueblo"];
  35. $cpostal = $json["cpostal"];
  36. $telefono = $json["telefono"];
  37. $correo = $json["correo"];
  38. # Checks if necessary data is empty
  39. if(strlen($nombre) == 0 or strlen($urbanizacion) == 0 or strlen($calle) == 0 or strlen($pueblo) == 0 or
  40. strlen($cpostal) == 0 or strlen($telefono) == 0 or strlen($correo) == 0 ){
  41. $conn->rollback();
  42. $conn->close();
  43. header("400 Bad Request", true, 400);
  44. exit();
  45. }
  46. # Inserts into miembros
  47. $sql = "INSERT INTO miembros (userid, nombre, organizacion, puesto, urbanizacion, calle, pueblo, cpostal, telefono, correo) VALUES "
  48. ."('".$userid."', '".$nombre."', '".$organizacion."', '".$puesto."', '".$urbanizacion.
  49. "', '".$calle."', '".$pueblo."', '".$cpostal."', '".$telefono."', '".$correo."')";
  50. if($conn->query($sql) === FALSE){
  51. $conn->rollback();
  52. $conn->close();
  53. header("500 Internal Server Error", true, 500);
  54. exit();
  55. }
  56. # Commits changes
  57. $conn->commit();
  58. $conn->close();
  59. # Returns 200
  60. header("200 OK", true, 200);
  61. exit();
  62. } else {
  63. header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405);
  64. exit();
  65. }
  66. ?>