Geen omschrijving

signup.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $username = isset($data->username) ? $data->username : '';
  13. $email = isset($data->email) ? $data->email : '';
  14. $password = isset($data->password) ? $data->password : '';
  15. // Clean user input
  16. $username = $connection->real_escape_string($username);
  17. $password = $connection->real_escape_string($password);
  18. $email = $connection->real_escape_string($email);
  19. function SignUp($username, $email, $password) {
  20. if ($username === '' || $password === '' || $email === '') {
  21. http_response_code(400); // use appropriate status
  22. echo json_encode(array("error" => "Fields 'username', 'email', and 'password' are required"));
  23. return;
  24. } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  25. http_response_code(400);
  26. echo json_encode(array("error" => "Invalid email '$email'"));
  27. return;
  28. }
  29. global $connection;
  30. $passwordHash = md5($password);
  31. $query = "INSERT INTO `UserData`
  32. (`username`, `email`, `password`)
  33. VALUES
  34. ('$username', '$email', '$passwordHash')";
  35. if ($connection->query($query)) {
  36. $user = array(
  37. "id" => (int) $connection->insert_id,
  38. "username" => $username,
  39. "email" => $email,
  40. );
  41. http_response_code(200);
  42. echo json_encode($user, JSON_UNESCAPED_UNICODE);
  43. } else {
  44. http_response_code(500); // use appropriate status
  45. echo json_encode(array("error" => $connection->error));
  46. }
  47. }
  48. // mail("vhernandezcastro@gmail.com", "test_signup_android", json_encode($data));
  49. SignUp($username, $email, $password);