In this video I am showing how to create user interface for User login and Registration pages. It is a very simple example and does not contain any complicated code.
Swift code to read user input data and to store it I am going to do in vedio 2
Here is how to create User login and Registration page for your iOS mobile app.
, I am writing Swift code to make User Login and Registration page work.
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
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 script3. Server side PHP script checks if user with provided user name and password exists4. Server side PHP script sends back a response to my mobile app in a JSON format.
2. Mobile app sends information to a server side PHP script3. Server side PHP script checks if user with provided user name and password exists4. 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
This file will contain database access details
2. MySQLDao.phpThis file will contain all MySQL queries
3. userRegister.phpBusiness logic to store user registration details into a database table
4. userLogin.phpBusiness 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
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(); ?>