Generally a service is one type of intermediary for communication between multiple different technologies, so here we will make a service which is used to log in valid users to Android applications.
Here the process flow is, the Android app user enters their username and password to the application then presses the login button; now on the login button we call PHP service which connects the My SQL database and checks the user table, and returns a response to the Android application.
For creating the Service in PHP we have to make one page in PHP and also make a table “tbl_User” for storing user data in My SQL, so by using the service we compare the user data coming from the Android app with tbl_User and return the result. For creating a service follow the below steps.
- Make Table in MySQL for
storing the user. Give it the name “tbl_User” -- this table contains
UserName, Password etc; For creating a table run the below script.
- CREATE TABLE IF NOT EXISTS `tbl_user` (
- `ur_Id` int(11) NOT NULL AUTO_INCREMENT,
- `ur_username` varchar(50) NOT NULL,
- `ur_password` varchar(50) NOT NULL,
- `ur_status` int(11) NOT NULL,
- PRIMARY KEY (`ur_Id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
- Now insert some temporary data to this table, so for inserting data in a temporary table I run the below SQL script.
- INSERT INTO `tbl_user` (`ur_Id`, `ur_username`, `ur_password`, `ur_status`) VALUES
- (1, 'nirav@gmail.com', 'nirav', 1),
- (2, 'kapil@gmail.com', 'kapil', 1),
- (3, 'arvind@gmail.com', 'arvind', 1);
- Now we make a PHP service, so create one PHP file and give it the name “service_userlogin.php”.
- <?php
- ?>
- Now we make MySQL server connection from the phpfile, so pass server name, user name, password, database name etc.
- $conn = new mysqli('localhost', 'root', '');
- mysqli_select_db($conn, 'EmployeeDB');
- Here we pass server name as ‘localhost’, username as ‘root’ , password to be blank(’’), and we pass database name as ‘EmployeeDB’.
- Now we get the parameter from the Android application so you get the parameter from the URL by using “GET” method. And check if it contains any value or not.
- if (isset($_GET[username]) && $_GET[username] != '' &&isset($_GET['password']) && $_GET['password'] != '')
- {
- $email = $_GET[username];
- $password = $_GET['password'];
- }
- We
check if the username and password comes from the service URL and if it
contains any data or not so first we store it in variable as shown in
the above code.
- Here we make a my SQL query for
checking if the user is valid or not and run this query and check the
output of query -- if there is a valid user then we pass the userid in
response of service.
- In service response we pass a total of three parameters.
Status
Message
UserId
- If user is valid then we pass Status=”Ok”, Message =”Login successfully”, UserId=”whatever user id you find”.
- If there is no user in the database with this userid and password then we pass a response with the following parameter like Status=”Not Ok” , Message=”Enter correct password” , UserId=”0”;
- $getData = "SELECT `ur_Id`,`ur_username`,`ur_password` FROM `tbl_user` WHERE `ur_username`='".$email.
- "'and `ur_password`='".$password.
- "'";
- $result = mysqli_query($conn, $getData);
- $userId = "";
- while ($r = mysqli_fetch_row($result))
- {
- $userId = $r[0];
- }
- if ($result - > num_rows > 0)
- {
- $resp["status"] = "1";
- $resp["userid"] = $userId;
- $resp["message"] = "Login successfully";
- }
- else
- {
- $resp["status"] = "-2";
- $resp["message"] = "Enter correct username or password";
- }
- Here finally our main logic is finished but now we have to make it set its “content-Type” parameter and pass it as JSON response.
- header('content-type: application/json');
- $response["response"]=$resp;
- echojson_encode($response);
- Now finally we close the MYSQL connection.
- @mysqli_close($conn);
- <?php
- $conn = new mysqli('localhost', 'root', '');
- mysqli_select_db($conn, 'EmployeeDB');
- if (isset($_GET[username]) && $_GET[username] != '' &&isset($_GET['password']) && $_GET['password'] != '')
- {
- $email = $_GET[username];
- $password = $_GET['password'];
- $getData = "SELECT `ur_Id`,`ur_username`,`ur_password` FROM `tbl_user` WHERE `ur_username`='" .$email."'
- and `ur_password`='".$password."'";
- $result = mysqli_query($conn,$getData);
- $userId="";
- while( $r = mysqli_fetch_row($result))
- {
- $userId=$r[0];
- }
- if ($result->num_rows> 0 ){
- $resp["status"] = "1";
- $resp["userid"] = $userId;
- $resp["message"] = "Login successfully";
- }
- else{
- $resp["status"] = "-2";
- $resp["message"] = "Enter correct username or password";
- }
- }
- else
- {
- $resp["status"] = "-2";
- $resp["message"] = "Enter Correct username.";
- }
- header('content-type: application/json');
- $response["response"]=$resp;
- echojson_encode($response);
- @mysqli_close($conn);
- ?>
- Now we run the php file in WAMP/XAMP server and pass username, password as parameter in URL.
http://localhost:8081/service_userlogin.php?username=nirav@gmail.com&password=nirav
Output- {"response":{"status":"1","userid":"1","message":"Login successfully"}}
Link Download
Tutorial via Youtube
LoginserviceinPHPandMySQLforAndroidApplication.zip
Comments
Post a Comment