Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WebService : http://localhost/register.php
- response : {"status":1,"message":"User already existed with armalsandip@gmail.com"}
- <?php
- require_once 'include/DB_Functions.php';
- $db = new DB_Functions();
- // json response array
- $response = array("status" => 1);
- if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['number'])) {
- // receiving the post params
- $name = $_POST['name'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- $number = $_POST['number'];
- // check if user is already existed with the same email
- if ($db->isUserExisted($email)) {
- // user already existed
- $response["status"] = 1;
- $response["message"] = "User already existed with " . $email;
- echo json_encode($response);
- } else {
- // create a new user
- $user = $db->storeUser($name, $email, $password, $number);
- if ($user) {
- // user stored successfully
- $response["status"] = 0;
- $response["id"] = $user["id"];
- $response["user"]["name"] = $user["name"];
- $response["user"]["email"] = $user["email"];
- $response["message"] = "User registered successfully with " . $email;
- echo json_encode($response);
- } else {
- // user failed to store
- $response["status"] = 2;
- $response["message"] = "Unknown error occurred in registration!";
- echo json_encode($response);
- }
- }
- } else {
- // receiving the post params
- $response["status"] = 3;
- $response["message"] = "Required parameters (name, email or password) is missing!";
- echo json_encode($response);
- }
- ?>
- public function storeUser($name, $email, $password, $number) {
- $hash = $this->hashSSHA($password);
- $encrypted_password = $hash["encrypted"]; // encrypted password
- $salt = $hash["salt"]; // salt
- $stmt = $this->conn->prepare("INSERT INTO users( name, email, number, encrypted_password, salt, created_at) VALUES(?, ?, ?, ?, ?, NOW())");
- $stmt->bind_param("sssss", $name, $email, $number, $encrypted_password, $salt);
- $result = $stmt->execute();
- $stmt->close();
- // check for successful store
- if ($result) {
- $stmt = $this->conn->prepare("SELECT * FROM users WHERE email = ?");
- $stmt->bind_param("s", $email);
- $stmt->execute();
- $user = $stmt->get_result()->fetch_assoc();
- $stmt->close();
- return $user;
- } else {
- return false;
- }
- }
- public function isUserExisted($email) {
- $stmt = $this->conn->prepare("SELECT email from users WHERE email = ?");
- $stmt->bind_param("s", $email);
- $stmt->execute();
- $stmt->store_result();
- if ($stmt->num_rows > 0) {
- // user existed
- $stmt->close();
- return true;
- } else {
- // user not existed
- $stmt->close();
- return false;
- }
- }
Add Comment
Please, Sign In to add comment