Login Service In PHP And MySQL For Android Applications

In this article, you will learn login service in PHP and MySQL for Android Applications.

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.

  1. 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.
    1. CREATE TABLE IF NOT EXISTS `tbl_user` (  
    2.   `ur_Id` int(11) NOT NULL AUTO_INCREMENT,  
    3.   `ur_username` varchar(50) NOT NULL,  
    4.   `ur_password` varchar(50) NOT NULL,  
    5.   `ur_status` int(11) NOT NULL,  
    6.   PRIMARY KEY (`ur_Id`)  
    7. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;  
    By running the above script it will create a table like below. Here table contains Id, Username, Password, Status etc, columns.



  2. Now insert some temporary data to this table, so for inserting data in a  temporary table I run the below SQL script.
    1. INSERT INTO `tbl_user` (`ur_Id`, `ur_username`, `ur_password`, `ur_status`) VALUES  
    2. (1, 'nirav@gmail.com''nirav', 1),  
    3. (2, 'kapil@gmail.com''kapil', 1),  
    4. (3, 'arvind@gmail.com''arvind', 1);  
    After running the below script you will see a table something like below.



  3. Now we make a PHP service, so create one PHP file and give it the name “service_userlogin.php”.
    1. <?php  
    2. ?>  
    • Now we make MySQL server connection from the phpfile, so pass server name, user name, password, database name etc.
      1. $conn = new mysqli('localhost''root''');  
      2. 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.
      1. if (isset($_GET[username]) && $_GET[username] != '' &&isset($_GET['password']) && $_GET['password'] != '')   
      2. {  
      3.    $email = $_GET[username];  
      4.    $password = $_GET['password'];   
      5. }  
    • 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”;
      1. $getData = "SELECT `ur_Id`,`ur_username`,`ur_password` FROM `tbl_user` WHERE `ur_username`='".$email.  
      2. "'and `ur_password`='".$password.  
      3. "'";  
      4. $result = mysqli_query($conn$getData);  
      5. $userId = "";  
      6. while ($r = mysqli_fetch_row($result))  
      7. {  
      8.     $userId = $r[0];  
      9. }  
      10. if ($result - > num_rows > 0)  
      11. {  
      12.     $resp["status"] = "1";  
      13.     $resp["userid"] = $userId;  
      14.     $resp["message"] = "Login successfully";  
      15. }  
      16. else  
      17. {  
      18.     $resp["status"] = "-2";  
      19.     $resp["message"] = "Enter correct username or password";  
      20. }  
    • 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.
      1. header('content-type: application/json');  
      2. $response["response"]=$resp;  
      3. echojson_encode($response);  
    • Now finally we close the MYSQL connection.
      1. @mysqli_close($conn);   
      Complete Code For PHP file “service_userlogin.php”
      1. <?php   
      2.     $conn = new mysqli('localhost''root''');  
      3.     mysqli_select_db($conn'EmployeeDB');  
      4.     if (isset($_GET[username]) && $_GET[username] != '' &&isset($_GET['password']) && $_GET['password'] != '')   
      5.     {  
      6.         $email = $_GET[username];  
      7.         $password = $_GET['password'];   
      8.   
      9.         $getData = "SELECT `ur_Id`,`ur_username`,`ur_password` FROM `tbl_user` WHERE `ur_username`='" .$email."'  
      10.         and `ur_password`='".$password."'";  
      11.   
      12.         $result = mysqli_query($conn,$getData);  
      13.   
      14.         $userId="";  
      15.         while$r = mysqli_fetch_row($result))  
      16.         {  
      17.             $userId=$r[0];  
      18.         }  
      19.   
      20.         if ($result->num_rows> 0 ){  
      21.   
      22.             $resp["status"] = "1";  
      23.             $resp["userid"] = $userId;  
      24.             $resp["message"] = "Login successfully";  
      25.         }  
      26.         else{  
      27.             $resp["status"] = "-2";  
      28.             $resp["message"] = "Enter correct username or password";  
      29.         }  
      30.   
      31.     }  
      32.     else  
      33.     {  
      34.   
      35.         $resp["status"] = "-2";  
      36.         $resp["message"] = "Enter Correct username.";  
      37.   
      38.   
      39.     }  
      40.   
      41.     header('content-type: application/json');  
      42.   
      43.     $response["response"]=$resp;  
      44.     echojson_encode($response);  
      45.   
      46.     @mysqli_close($conn);  
      47.   
      48. ?>  
    • 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
      1. {"response":{"status":"1","userid":"1","message":"Login successfully"}}  

Thanks for reading my article; if you have any question regarding this than you can ask me in the comment section.