Guest User

Untitled

a guest
Oct 2nd, 2018
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. WebService : http://localhost/register.php
  2.  
  3. response : {"status":1,"message":"User already existed with armalsandip@gmail.com"}
  4.  
  5. <?php
  6.  
  7. require_once 'include/DB_Functions.php';
  8. $db = new DB_Functions();
  9.  
  10. // json response array
  11. $response = array("status" => 1);
  12.  
  13. if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['number'])) {
  14.  
  15. // receiving the post params
  16. $name = $_POST['name'];
  17. $email = $_POST['email'];
  18. $password = $_POST['password'];
  19. $number = $_POST['number'];
  20.  
  21. // check if user is already existed with the same email
  22. if ($db->isUserExisted($email)) {
  23. // user already existed
  24. $response["status"] = 1;
  25. $response["message"] = "User already existed with " . $email;
  26. echo json_encode($response);
  27. } else {
  28. // create a new user
  29. $user = $db->storeUser($name, $email, $password, $number);
  30. if ($user) {
  31. // user stored successfully
  32. $response["status"] = 0;
  33. $response["id"] = $user["id"];
  34. $response["user"]["name"] = $user["name"];
  35. $response["user"]["email"] = $user["email"];
  36. $response["message"] = "User registered successfully with " . $email;
  37. echo json_encode($response);
  38. } else {
  39. // user failed to store
  40. $response["status"] = 2;
  41. $response["message"] = "Unknown error occurred in registration!";
  42. echo json_encode($response);
  43. }
  44. }
  45. } else {
  46. // receiving the post params
  47. $response["status"] = 3;
  48. $response["message"] = "Required parameters (name, email or password) is missing!";
  49. echo json_encode($response);
  50. }
  51. ?>
  52.  
  53. public function storeUser($name, $email, $password, $number) {
  54. $hash = $this->hashSSHA($password);
  55. $encrypted_password = $hash["encrypted"]; // encrypted password
  56. $salt = $hash["salt"]; // salt
  57.  
  58. $stmt = $this->conn->prepare("INSERT INTO users( name, email, number, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
  59. $stmt->bind_param("sssss", $name, $email, $number, $encrypted_password, $salt);
  60. $result = $stmt->execute();
  61. $stmt->close();
  62.  
  63. // check for successful store
  64. if ($result) {
  65. $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
  66. $stmt->bind_param("s", $email);
  67. $stmt->execute();
  68. $user = $stmt->get_result()->fetch_assoc();
  69. $stmt->close();
  70.  
  71. return $user;
  72. } else {
  73. return false;
  74. }
  75. }
  76.  
  77. public function isUserExisted($email) {
  78. $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
  79.  
  80. $stmt->bind_param("s", $email);
  81.  
  82. $stmt->execute();
  83.  
  84. $stmt->store_result();
  85.  
  86. if ($stmt->num_rows > 0) {
  87. // user existed
  88. $stmt->close();
  89. return true;
  90. } else {
  91. // user not existed
  92. $stmt->close();
  93. return false;
  94. }
  95. }
Add Comment
Please, Sign In to add comment