Login Form

In order to login, you must have created a database and a table. Hence, it is advised that you first execute the Registration Form program because here we will be using the UserLoginTable, and DatabaseConfig.php created in the Registration Form program.

Create login.php file inside public_html folder and write the following code.

<?php
	include 'DatabaseConfig.php';

	$email = isset($_POST['email']) ? $_POST["email"] : '';;
	$password = isset($_POST['password']) ? $_POST["password"] : '';;

	$Sql_Query = "select * from UserLoginTable where email = '$email' and password = '$password' ";
	$check = mysqli_fetch_array(mysqli_query($con,$Sql_Query));
	if(isset($check))
	{
	    echo "Success";
	}
	else
	{
	    echo "Invalid Username or Password Please Try Again";
	}
	mysqli_close($con);
?>

Step 1: Start an android project.

Step 2: Design the first page as shown in the figure.

Step 3: Open the AndroidManifest.xml file and add the following line after the application tag.

<uses-permission android:name="android.permission.INTERNET" />

Step 4: Open build.gradle(Module:………..) file, inside dependencies, add the following code and synchronize the project.

implementation 'com.android.volley:volley:1.1.1

Step 5: Create a second page and design it with one textview to display a message “Login Successful”.

Step 6: Open MainActivity.java and add the following code.

package com.example.part_b6_login_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;


import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
// 1. Add <uses-permission android:name="android.permission.INTERNET" /> in
// AndroidManifest.xml just before <application tag
// 2. Add implementation 'com.android.volley:volley:1.2.1' in build.gradle (:app)
    //3. Add php file to web server

    EditText Email,Password;
    TextView login_fail;
    Button login;
    String PasswordHolder, EmailHolder;
    String webhost_path="YOUR_WEBSITE_URL/login.php";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Assign Id'S
        Email = (EditText) findViewById(R.id.txt_email);
        Password = (EditText) findViewById(R.id.txt_password);

        login = (Button) findViewById(R.id.btn_login);
        TextView text=(TextView)findViewById(R.id.textView2);

        login_fail=(TextView)findViewById(R.id.txt_login_fail);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EmailHolder = Email.getText().toString();
                PasswordHolder = Password.getText().toString();

                login_User(EmailHolder,PasswordHolder);

            }
        });

    }
    public void login_User(String EmailHolder, String PasswordHolder)
    {
        final StringRequest stringRequest1=new StringRequest(Request.Method.POST, webhost_path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try{
                    if(response.equals("Success"))
                    {
                        login_fail.setText("");
                        Toast.makeText(getApplicationContext(),"Login Successful", Toast.LENGTH_LONG).show();
                        Intent in=new Intent(MainActivity.this, Second_Page.class);
                        startActivity(in);
                    }else{
                        login_fail.setText("Login failed, check user name and password");
                        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(), "Exception occurred", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "Login error", Toast.LENGTH_SHORT).show();
            }
        })
        {
            protected Map<String,String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("email", EmailHolder);
                params.put("password", PasswordHolder);
                return params;
            }
        };

        final RequestQueue requestQueue1 = Volley.newRequestQueue(MainActivity.this);
        requestQueue1.add(stringRequest1);
    }

}

Step 7: Run the project.