SHARE
TWEET

Untitled

a guest Apr 20th, 2018 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class DbOperation {
  2.  
  3. private $connect;
  4.  
  5. //Constructor
  6. function __construct() {
  7.     require_once dirname(__FILE__). '/Constants.php';
  8.     require_once dirname(__FILE__). '/DbConnect.php';
  9.  
  10.     //opening db connection
  11.     $database = new DbConnect();
  12.     $this->connect = $database->connect();
  13. }
  14.  
  15. //This method is taking a username and password and verifying it from the database
  16. public function userLogin($username, $pass) {
  17.     $pass = md5($pass);
  18.     $statement = $this->connect->prepare("SELECT id FROM users WHERE username = ? AND password = ?");
  19.     $statement->bind_param("ss", $username, $password);
  20.     $statement->execute();
  21.     $statement->store_result();
  22.     return $statement->num_rows > 0;
  23. }
  24.  
  25. //After the successful login we will call this method to return the user data in an array
  26. public function getUserByUsername($username) {
  27.     $statement = $this->connect->prepare("SELECT id, username, email FROM users WHERE username = ?");
  28.     $statement->bind_param("s", $username);
  29.     $statement->execute();
  30.     $statement->bind_result($id, $uname, $email);
  31.     $statement->fetch();
  32.     $user = array();
  33.     $user['id'] = $id;
  34.     $user['username'] = $uname;
  35.     $user['email'] = $email;
  36.     return $user;
  37. }
  38.  
  39. //Function to create a new user
  40. public function createUser($username, $password, $email, $name) {
  41.     if (!$this->isUserExist($username, $email)) {
  42.         $pass = md5($password);
  43.         $statement = $this->connect->prepare("INSERT INTO users (username, password, email, name) VALUES (?, ?, ?, ?)");
  44.         $statement->bind_param("sssss", $username, $pass, $email, $name);
  45.         if ($statement->execute()) {
  46.             return USER_CREATED;
  47.         } else {
  48.             return USER_NOT_CREATED;
  49.         }
  50.     } else {
  51.         return USER_ALREADY_EXIST;
  52.         }        
  53.     }
  54.  
  55. private function isUserExist($username, $email) {
  56.     $statement = $this->connect->prepare("SELECT id FROM users WHERE username = ? OR email = ?");
  57.     $statement->bind_param("sss", $username, $email);
  58.     $statement->execute();
  59.     $statement->store_result();
  60.     return $statement->num_rows > 0;
  61. }
  62.    
  63. //importing required script
  64. require_once './includes/DbOperation.php';
  65.  
  66. $response = array();
  67.  
  68. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  69. if (!verifyRequiredParams(array('username', 'password', 'email', 'name'))) {
  70.  
  71.     //getting values
  72.     $username = $_POST['username'];
  73.     $password = $_POST['password'];
  74.     $email = $_POST['email'];
  75.     $name = $_POST['name'];
  76.  
  77.     //creating database operation on object
  78.     $database = new DbOperation();
  79.  
  80.     //adding user to database
  81.     $result = $database->createUser($username, $password, $email, $name);
  82.  
  83.     //making the response accordingly
  84.     if ($result == USER_CREATED) {
  85.         $response['error'] = false;
  86.         $response['message'] = 'User was created successfully';
  87.     } elseif ($result == USER_ALREADY_EXIST) {
  88.         $response['error'] = true;
  89.         $response['message'] = 'This user already Exist!';
  90.     } elseif ($result == USER_NOT_CREATED) {
  91.         $response['error'] = true;
  92.         $response['message'] = 'Some error occurred';
  93.     }
  94.     } else {
  95.         $response['error'] = true;
  96.         $response['message'] = 'Required parameters are missing';
  97.     }
  98. } else {
  99.     $response['error'] = true;
  100.     $response['message'] = 'Invalid request';
  101. }
  102.  
  103.  
  104.  
  105. //function to validate the required paramter in request
  106. function verifyRequiredParams($required_fields) {
  107.  
  108. //Getting the request parameters
  109. $request_params = $_REQUEST;
  110.  
  111. //Looping through all the parameters
  112. foreach ($required_fields as $field) {
  113.     //if any required parameter is missing
  114.     if (!isset($request_params[$field]) ||      strlen(trim($request_params[$field])) <= 0) {
  115.  
  116.         //return true
  117.         return true;
  118.     }
  119.  }
  120.   return false;
  121. }
  122.  
  123.  
  124. echo json_encode($response);
  125.    
  126. @objc func registerUser(button: UIButton) {
  127.  
  128.     let username = userName.text!
  129.     let password = passWord.text!
  130.     let email = emailField.text!
  131.     let name = nameField.text!
  132.  
  133.     //Creating parameters for post request
  134.  
  135.     let parameters: Parameters = ["username": username, "password": password, "email": email, "name": name]
  136.  
  137.     guard let url = URL(string: "http://localhost/TestingiOSApp/v1/register.php") else { return }
  138.  
  139.     var request = URLRequest(url: url)
  140.     request.httpMethod = "POST"
  141.     request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  142.     guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
  143.     request.httpBody = httpBody
  144.  
  145.     let session = URLSession.shared
  146.     session.dataTask(with: request) { (data, response, error) in
  147.         if let response = response {
  148.             print(response)
  149.         }
  150.  
  151.         if let data = data {
  152.             do {
  153.                 let json = try JSONSerialization.jsonObject(with: data, options: [])
  154.                 print(json)
  155.             } catch {
  156.                 print(error)
  157.             }
  158.         }
  159.     }.resume()
  160.  
  161.     self.view.endEditing(true)
  162. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top