Android Login Registration using PHP and MySQL

Android Login Registration using PHP and MySQL

Hey Technoz! In this tutorial, we are going to see how to build android login registration app using PHP and MySQL as a back end system. You can use any other language and database as a back end. But, as PHP and MySQL are most common and popular, we will be using it in this tutorial. This Android Login Registration using PHP and MySQL tutorial covers creating a new user registration, Logging in already registered student, and logging out also. So lets start.

Creating Server side

In our project, the app will be a client sending requests to the server and the PHP files and database will act as a back end server. Thus we are creating two parts in our android login registration tutorial.

Creating Database

Obviously, we first need a database to store and retrieve data. So create a database and table users in it. The table contains fields as id, username, email and password. You can use the following SQL query.

Creating PHP Files

Of course, we need a separate php file to establish a connection with the database. So create file config.php and paste the code below in it.

Remember to change the connection values with yours.

Now, we will create a separate file which does the working of logging in the user. Thus create a file login.php with the following code.

As the android login registration app requires data in JSON format, we are using an array here. At first, we will receive the POST variables and check if the user exists in database and perform a password verification. If the credentials are right, we will put the success message in array variable. Lastly, we will echo the array as a JSON response.

Now its time for a registration task. Similarly, we will create a php file register.php with the following code in it.

Similar to the login file, we have created the above file for registration task. Here we are accepting username, email and password as a POST parameters. As usual, we are inserting the received data in the database with password encrypted. The success/failure message is sent using array variables as a JSON response.

Thus, we have completed the server side part. Now we will move towards the client side android app in android login registration.

Creating Client side

Now we have to build an android app which will take the values entered by user, send it to specific urls, get back the results and show to the user. So lets start.

We will be using http requests to send/receive data. For that purpose, we need to add the below line in app-level build.gradle file.

Furthermore, we need to add the internet permission in AndroidManifest.xml file as follows.

At first, we will build a special class which will accept the URLs and POST data parameters, send them to their respective places, and return the results received. So create a new java class with following code in it.

The method sendPostRequest() accepts the url and post parameters. We are using a HashMap data structure here to get and set key and value pairs. We are here building a string using StringBuilder class and append the response received, to the string. The method getPostDataString() accepts HashMap parameters and appends them with the url.

Creating Activities

Registration Activity

Now, we will create the registration activity. Create a new empty activity and write below code in activity_register.xml file.

We have created three EditText for username, email, password and a submit button for android login registration. See the image below.

Android Login Registration using PHP and MySQL

Now lets build the file as follows.

The above class takes the values entered from user and the values are sent to the sendPostRequest method in the RequestHandler class. The RequestHandler class then does the work of sending and receiving data and we print the response message by toast text. We do this process using AsyncTask which helps to do things in background. You can learn more about AsyncTask Here. The values entered by user will be stored using PHP and MySQL database.

Login Activity

Furthermore, we move towards the LoginActivity. At first, create layout using activity_login.xml file with the following code in it.

Above file will create two EditText input fields for email and password and a submit button as shown below.

Android Login Registration using PHP and MySQL

Lets have a look at java file below for android login registration tutorial.

Similar to the Login file, we are taking input values from user and sending them using RequestHandler class. The server side php scripts will check for valid credentials and return the results. If the result is true, we will open a new welcome activity. Here, we are taking care that once the user is logged in, he/she will not need to login again. For this purpose, we are here using SharedPreferences data structure. It is a key-value paired data structure which stores the small amount of data in them.

Welcome Activity

After fetching the data we will store it in SharedPreferences. The ‘status’ key contains a Boolean value which will set  to 1 after successful login. The next time user will open the LoginActivity, he/she will be directly redirected to the Welcome screen. We are also storing the email, username and password after login. Now lets create a new activity WelcomeActivity which will open only after successful login. Below code of activity_welcome.xml.

The java code of in Android Login Registration using PHP and MySQL tutorial is as follows.

We are showing here username of candidate and a logout button. Obviously, the logout button will clear all the values in SharedPreferences and redirect user to the login screen. See the screenshot below.

Android Login Registration using PHP and MySQL

Thus, we have completed the Android Login Registration using PHP and MySQL tutorial. Hope you understand it. Furthermore, if you have any doubts, feel free to ask in comment below. You can always download the source code from below link.

Download Source Code

14 thoughts on “Android Login Registration using PHP and MySQL

  1. Akinyemi john says:

    A good tutorial you have here but i have questions see below
    Is there a way to prevent a user from using another persons
    crdentials i.e preventing user A from using user B credentials on his device unless he registers. Is there a way
    to make the username or email or password to be used only once. If the user B wants to use user A username or email or password
    the app should prompt or show dialog “username or email or password already in use by another user”. Kindly help

    1. Ashish Joshi says:

      Good idea. Yes we can do it by setting a boolean value after logging in and should not permit another user to login unless this flag is unset. We will unset the flag only after user loggs out from device.

    1. d
      datapalace2019 says:

      the code below is what you need to edit
      public static final String URL_LOGIN = “”;

      change the IP to yours but make sure you dnt replace the IP with “localhost”

      Open the Command Prompt by clicking Start and search CMD then click cmd.exe.
      Type ipconfig /all

      the above list IPs, locate IP like 192.168..

      then the problem is solved

  2. S
    Sandeep says:

    HI sir , it is showing error org.json.jsonexception end of input at character 0 of, im using not import; not import;

    Please advice me how to solve this?.

    1. S
      Sandeep says:

      * i’m using import; not import;

  3. d
    datapalace2019 says:

    Thanks so much. I was able to submit to database but the TOAST is returning error

    org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject.

    Please what am i getting wrong? what is needed to correct this.

    Thanks once again

  4. d
    datapalace2019 says:

    Never Mind, Ive solved this by putting off error report in my PHP serveride script.

    I put error_reporting(0) at the top of login.php and regiser.php.

    I am really enjoying your tutorials. Maybe i will come and visit you in India for more tutorial. Smiles..


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.