Нет описания

signup.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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) or strlen($urbanizacion) or strlen($calle) or strlen($pueblo) or strlen($cpostal) or strlen($telefono) or strlen($correo))){
  40. $conn->rollback();
  41. $conn->close();
  42. header("400 Bad Request", true, 400);
  43. exit();
  44. }
  45. # Inserts into miembros
  46. $sql = "INSERT INTO miembros (userid, nombre, organizacion, puesto, urbanizacion, calle, pueblo, cpostal, telefono, correo) VALUES "
  47. ."('".$userid."', '".$nombre."', '".$organizacion."', '".$puesto."', '".$urbanizacion.
  48. "', '".$calle."', '".$pueblo."', '".$cpostal."', '".$telefono."', '".$correo."')";
  49. if($conn->query($sql) === FALSE){
  50. $conn->rollback();
  51. $conn->close();
  52. header("500 Internal Server Error", true, 500);
  53. exit();
  54. }
  55. # Commits changes
  56. $conn->commit();
  57. $conn->close();
  58. # Returns 200
  59. header("200 OK", true, 200);
  60. exit();
  61. } else {
  62. header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed", true, 405);
  63. exit();
  64. }
  65. ?>