Often we need to store data our mobile app collects from a user into a database. For example, when implementing user registration feature we need to store user details. Earlier I have created two video tutorials on how to implement User Registration and Login:
User Registration and Login example Video #1
User Registration and Login example Video #2
However in these two examples I do not store user details on server side and I do not use server side at all. This blog post is going to be my example Video #3 and I will use PHP and MySQL to store user details on server side and implement fully functional User Registration and Login flow. Please keep in mind that this is a very simplified example. User registration form might be a lot more lengthy than the one I create here and in your case the business logic itself might require additional user details validation which I do not do here for the sake of simplicity.
Most of the time when I work on User Registration and Login flow for my project these are the basic steps I follow:
User registration flow:
1. User fills in Register form in your mobile app and submits information to a server side PHP script
2. Server side PHP script accepts user registration information and stores it in MySQL database
3. Server side PHP script sends back response to mobile app in JSON format.
User login flow:
1. User types in user name and password in your mobile app and taps on a login button to login
2. Mobile app sends information to a server side PHP script
3. Server side PHP script checks if user with provided user name and password exists
4. Server side PHP script sends back a response to my mobile app in a JSON format.
Let’s assume we have MySQL database created and it is called “swift_developer”. In our database we have a table called “users” with a very simple structure:
+---------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_email | varchar(50) | NO | | NULL | |
| user_password | varchar(32) | NO | | NULL | |
+---------------+-------------+------+-----+---------+----------------+
To connect to this database I am going to create 4 PHP scripts:
1. Conn.php
This file will contain database access details
2. MySQLDao.php
This file will contain all MySQL queries
3. userRegister.php
Business logic to store user registration details into a database table
4. userLogin.php
Business logic to check if user with provided user name and password exist in our database
Now, let’s implement Conn.php script
<?php class Conn { public static $dbhost = "localhost"; public static $dbuser = "< provide here user name to your database>; public static $dbpass = "< password you use to access database >"; public static $dbname = " <database name> "; } ?>
Next, lets implement MySQLDao.php
<?php class MySQLDao { var $dbhost = null; var $dbuser = null; var $dbpass = null; var $conn = null; var $dbname = null; var $result = null; function __construct() { $this->dbhost = Conn::$dbhost; $this->dbuser = Conn::$dbuser; $this->dbpass = Conn::$dbpass; $this->dbname = Conn::$dbname; } public function openConnection() { $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); if (mysqli_connect_errno()) echo new Exception("Could not establish connection with database"); } public function getConnection() { return $this->conn; } public function closeConnection() { if ($this->conn != null) $this->conn->close(); } public function getUserDetails($email) { $returnValue = array(); $sql = "select * from users where user_email='" . $email . "'"; $result = $this->conn->query($sql); if ($result != null && (mysqli_num_rows($result) >= 1)) { $row = $result->fetch_array(MYSQLI_ASSOC); if (!empty($row)) { $returnValue = $row; } } return $returnValue; } public function getUserDetailsWithPassword($email, $userPassword) { $returnValue = array(); $sql = "select id,user_email from users where user_email='" . $email . "' and user_password='" .$userPassword . "'"; $result = $this->conn->query($sql); if ($result != null && (mysqli_num_rows($result) >= 1)) { $row = $result->fetch_array(MYSQLI_ASSOC); if (!empty($row)) { $returnValue = $row; } } return $returnValue; } public function registerUser($email, $password) { $sql = "insert into users set user_email=?, user_password=?"; $statement = $this->conn->prepare($sql); if (!$statement) throw new Exception($statement->error); $statement->bind_param("ss", $email, $password); $returnValue = $statement->execute(); return $returnValue; } } ?>
We can now make use of these two scripts to write business logic to register a new user:
userRegister.php
<?php require("Conn.php"); require("MySQLDao.php"); $email = htmlentities($_POST["email"]); $password = htmlentities($_POST["password"]); $returnValue = array(); if(empty($email) || empty($password)) { $returnValue["status"] = "error"; $returnValue["message"] = "Missing required field"; echo json_encode($returnValue); return; } $dao = new MySQLDao(); $dao->openConnection(); $userDetails = $dao->getUserDetails($email); if(!empty($userDetails)) { $returnValue["status"] = "error"; $returnValue["message"] = "User already exists"; echo json_encode($returnValue); return; } $secure_password = md5($password); // I do this, so that user password cannot be read even by me $result = $dao->registerUser($email,$secure_password); if($result) { $returnValue["status"] = "Success"; $returnValue["message"] = "User is registered"; echo json_encode($returnValue); return; } $dao->closeConnection(); ?>
and the final script is to check is to check is user is found in our table of registered users or not:
userLogin.php
<?php require("Conn.php"); require("MySQLDao.php"); $email = htmlentities($_POST["email"]); $password = htmlentities($_POST["password"]); $returnValue = array(); if(empty($email) || empty($password)) { $returnValue["status"] = "error"; $returnValue["message"] = "Missing required field"; echo json_encode($returnValue); return; } $secure_password = md5($password); $dao = new MySQLDao(); $dao->openConnection(); $userDetails = $dao->getUserDetailsWithPassword($email,$secure_password); if(!empty($userDetails)) { $returnValue["status"] = "Success"; $returnValue["message"] = "User is registered"; echo json_encode($returnValue); } else { $returnValue["status"] = "error"; $returnValue["message"] = "User is not found"; echo json_encode($returnValue); } $dao->closeConnection(); ?>
Also, I have recorded a video showing you how this example works. User Registration and Login video example #3 using Swift, PHP and MySQL.
This is it!
I hope this blog post is helpful to you! 🙂
[raw_html_snippet id=”cookbookpagecoursesheader”]
Unit Testing Swift Mobile App
Apply Test-Driven Development(TDD) process to iOS mobile app development in Swift Preview this video course.