Complete Registration and Login form in PHP and MySql Database
Complete User Registration and Login form in PHP and MySql Database
Login and Registration form in php and mysql is most needed in every IT Projects. So I am here to guide you the steps for making login and registration page in php, Android Studio Software. After steps here i will provide you source code in PHP using MySQL database. All dependencies of Mysql database will be provided.
Download Android Studio Software and implement the project. Here you will get the source code of Java and xml language of project.
Steps for Login and Registration in php and mysql-
Add XML File
- activity_main.xml –
<?xml version=”1.0″ encoding=”UTF-8″?>
-<ScrollView android:fillViewport=”true” android:layout_height=”fill_parent” android:layout_width=”fill_parent” xmlns:android=”http://schemas.android.com/apk/res/android”>
-<RelativeLayout android:layout_height=”wrap_content” android:layout_width=”match_parent”>
-<LinearLayout android:layout_height=”wrap_content” android:layout_width=”match_parent” android:id=”@+id/linearLayout” android:paddingRight=”24dp” android:paddingLeft=”24dp” android:layout_centerInParent=”true” android:orientation=”vertical”>
<EditText android:layout_height=”wrap_content” android:layout_width=”match_parent” android:id=”@+id/editName” android:focusableInTouchMode=”true” android:focusable=”true” android:fontFamily=”sans-serif-light” android:textColorHint=”#A0192133″ android:textColor=”#FF192133″ android:hint=”Username”/>
<EditText android:layout_height=”wrap_content” android:layout_width=”match_parent” android:id=”@+id/editPassword” android:focusableInTouchMode=”true” android:focusable=”true” android:fontFamily=”sans-serif-light” android:textColorHint=”#A0192133″ android:textColor=”#FF192133″ android:hint=”Password” android:inputType=”textPassword”/>
<EditText android:layout_height=”wrap_content” android:layout_width=”match_parent” android:id=”@+id/editEmail” android:focusableInTouchMode=”true” android:focusable=”true” android:fontFamily=”sans-serif-light” android:textColorHint=”#A0192133″ android:textColor=”#FF192133″ android:hint=”Email” android:inputType=”textEmailAddress” android:visibility=”gone”/>
<Button android:layout_height=”wrap_content” android:layout_width=”match_parent” android:id=”@+id/btnSignIn” android:textStyle=”bold” android:text=”SIGN IN”/>
<Button android:layout_height=”wrap_content” android:layout_width=”match_parent” android:id=”@+id/btnRegister” android:textStyle=”bold” android:text=”REGISTER”/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Add JAVA File
- Main_activity.java –
package com.alldetailinfo.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 java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText editEmail, editPassword, editName;
Button btnSignIn, btnRegister;
String URL= “http://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<NameValuePair> params = new ArrayList<NameValuePair>();
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();
}
}
}
}
Add PHP File
- index.php
<?php
require_once ‘user.php’;
$username = “”;
$password = “”;
$email = “”;
if(isset($_POST[‘username’])){
$username = $_POST[‘username’];
}
if(isset($_POST[‘password’])){
$password = $_POST[‘password’];
}
if(isset($_POST[’email’])){
$email = $_POST[’email’];
}
$userObject = new User();
// Registration
if(!empty($username) && !empty($password) && !empty($email)){
$hashed_password = md5($password);
$json_registration = $userObject->createNewRegisterUser($username, $hashed_password, $email);
echo json_encode($json_registration);
}
// Login
if(!empty($username) && !empty($password) && empty($email)){
$hashed_password = md5($password);
$json_array = $userObject->loginUsers($username, $hashed_password);
echo json_encode($json_array);
}
?>
2. user.php –
<?php
include_once ‘db-connect.php’;
class User{
private $db;
private $db_table = “users”;
public function __construct(){
$this->db = new DbConnect();
}
public function isLoginExist($username, $password){
$query = “select * from “.$this->db_table.” where username = ‘$username’ AND password = ‘$password’ Limit 1″;
$result = mysqli_query($this->db->getDb(), $query);
if(mysqli_num_rows($result) > 0){
mysqli_close($this->db->getDb());
return true;
}
mysqli_close($this->db->getDb());
return false;
}
public function isEmailUsernameExist($username, $email){
$query = “select * from “.$this->db_table.” where username = ‘$username’ AND email = ‘$email'”;
$result = mysqli_query($this->db->getDb(), $query);
if(mysqli_num_rows($result) > 0){
mysqli_close($this->db->getDb());
return true;
}
return false;
}
public function isValidEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
public function createNewRegisterUser($username, $password, $email){
$isExisting = $this->isEmailUsernameExist($username, $email);
if($isExisting){
$json[‘success’] = 0;
$json[‘message’] = “Error in registering. Probably the username/email already exists”;
}
else{
$isValid = $this->isValidEmail($email);
if($isValid)
{
$query = “insert into “.$this->db_table.” (username, password, email, created_at, updated_at) values (‘$username’, ‘$password’, ‘$email’, NOW(), NOW())”;
$inserted = mysqli_query($this->db->getDb(), $query);
if($inserted == 1){
$json[‘success’] = 1;
$json[‘message’] = “Successfully registered the user”;
}else{
$json[‘success’] = 0;
$json[‘message’] = “Error in registering. Probably the username/email already exists”;
}
mysqli_close($this->db->getDb());
}
else{
$json[‘success’] = 0;
$json[‘message’] = “Error in registering. Email Address is not valid”;
}
}
return $json;
}
public function loginUsers($username, $password){
$json = array();
$canUserLogin = $this->isLoginExist($username, $password);
if($canUserLogin){
$json[‘success’] = 1;
$json[‘message’] = “Successfully logged in”;
}else{
$json[‘success’] = 0;
$json[‘message’] = “Incorrect details”;
}
return $json;
}
}
?>
3. db_connect.php –
<?php
include_once ‘config.php’;
class DbConnect{
private $connect;
public function __construct(){
$this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno($this->connect)){
echo “Unable to connect to MySQL Database: ” . mysqli_connect_error();
}
}
public function getDb(){
return $this->connect;
}
}
?>
Download source code from this link-
Login and Registration in PHP using Android Studio (MySQL Database)
If you have any question related this article then comment down below or you can email us at
alldetailtech@gmail.com
Pingback:Login and Registration Page in Firebase, PHP, MySql, Jquery and ASP.NET