Developing an Android App for Login and Registration with PHP and MySQL
In today’s digital world, login and registration functionalities are ubiquitous in apps. They enable managing user information and offering personalized experiences. In this tutorial, we will set up a local web server and a MySQL database to develop an Android app for login and registration. We will use PHP scripts to establish a connection to the MySQL database.
Setting Up the XAMPP Server
Firstly, we need to create a backend web server. On a Mac operating system, we can use XAMPP, which quickly sets up a local Apache web server and a MySQL database. After installing and starting XAMPP, we can check the server’s functionality via https://localhost.
<?php
echo "Hello, World";
?>
Setting Up the MySQL Database
Using phpMyAdmin, accessible at https://localhost/phpmyadmin, we can create a MySQL database. After creating an empty database, we can create a table named “users” to store user data.
CREATE TABLE IF NOT EXISTS `firstDB`.`users` (
`id` int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(70) NOT NULL,
`password` varchar(40) NOT NULL,
`email` varchar(50) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime DEFAULT NULL
)
Connecting PHP to the MySQL Database
To establish a connection between a PHP script and the MySQL database, we need three input values: hostname, MySQL username, and MySQL password.
<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_NAME", "firstDB");
?>
Android Login and Registration App
After setting up the basics for PHP and MySQL, we can focus on developing the Android login application. We will create an application for user login and registration. During registration, we will verify the uniqueness of the username and email address. We will use PHP scripts and the MySQL database for this purpose.
Project Structure and Code for the Android Login and Registration App
The project structure includes various classes and resources for the user interface. In the MainActivity.java class, we handle user inputs and perform asynchronous tasks for the network connection. The JSONParser.java class handles communication with the server and parsing JSON data.
Conclusion and Project Download
With this tutorial, we have gained a basic understanding of implementing login and registration functionalities in an Android app using PHP and MySQL. The complete project with all PHP scripts and the Android app can be downloaded via the link provided below.
package com.journaldev.loginphpmysql;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText editEmail, editPassword, editName;
Button btnSignIn, btnRegister;
String URL= "https://10.0.3.2/test_android/index.php";
JSONParser jsonParser=new JSONParser();
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editEmail=(EditText)findViewById(R.id.editEmail);
editName=(EditText)findViewById(R.id.editName);
editPassword=(EditText)findViewById(R.id.editPassword);
btnSignIn=(Button)findViewById(R.id.btnSignIn);
btnRegister=(Button)findViewById(R.id.btnRegister);
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AttemptLogin attemptLogin= new AttemptLogin();
attemptLogin.execute(editName.getText().toString(),editPassword.getText().toString(),"");
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(i==0)
{
i=1;
editEmail.setVisibility(View.VISIBLE);
btnSignIn.setVisibility(View.GONE);
btnRegister.setText("CREATE ACCOUNT");
}
else{
btnRegister.setText("REGISTER");
editEmail.setVisibility(View.GONE);
btnSignIn.setVisibility(View.VISIBLE);
i=0;
AttemptLogin attemptLogin= new AttemptLogin();
attemptLogin.execute(editName.getText().toString(),editPassword.getText().toString(),editEmail.getText().toString());
}
}
});
}
private class AttemptLogin extends AsyncTask<String, String, JSONObject> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONObject doInBackground(String... args) {
String email = args[2];
String password = args[1];
String name= args[0];
ArrayList params = new ArrayList();
params.add(new BasicNameValuePair("username", name));
params.add(new BasicNameValuePair("password", password));
if(email.length()>0)
params.add(new BasicNameValuePair("email",email));
JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params);
return json;
}
protected void onPostExecute(JSONObject result) {
// dismiss the dialog once product deleted
//Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
try {
if (result != null) {
Toast.makeText(getApplicationContext(),result.getString("message"),Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
In this tutorial, we have gained a basic understanding of implementing login and registration functionalities in an Android app using PHP and MySQL. With this tutorial and the provided project, you are well-equipped to develop your own applications with login and registration functionalities. Good luck!