How to Registraion Using Volley (Php and Mysql) After Check User Registration or not in Android studio
Registration is an essential aspect of any app that requires user data. To ensure that the app is secure and reliable, you need to check if a user is registered before allowing them to access the app's features. One way to achieve this is by using Volley, a popular HTTP library that simplifies networking in Android applications. Here's how to implement user registration and validation using Volley and PHP/MySQL:
First, create a PHP script that will accept user registration data, check if the user already exists in the database, and store the data if the user is new. Use password hashing to protect the user's password.
Next, create an Android registration activity that allows users to input their registration details. Use Volley to send the user's data to the PHP script and receive the server's response.
In the server's response, check the success parameter to determine if the registration was successful or not. If successful, allow the user to log in and access the app's features. If not, display an appropriate error message.
In conclusion, using Volley, PHP, and MySQL to register and validate users in an Android app is an efficient way to ensure that your app is secure and reliable. By following the steps outlined above, you can easily implement user registration and validation in your Android app.
Here is my Code:
.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Login_Reg.Registration"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="vertical">
<soup.neumorphism.NeumorphCardView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="নতুন এ্যাকাউন্ট তৈরী করতে প্রয়োজনীয় তথ্য দিন।"
android:fontFamily="@font/solaimanlipi"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="#870581"
android:textSize="20dp"
android:visibility="gone"/>
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:neumorph_backgroundColor="#279EFA">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:text="রেজিস্ট্রেশন করুন"
android:textAlignment="center"
android:fontFamily="@font/solaimanlipi"
android:textStyle="bold"
android:textColor="@color/white"
android:textSize="30dp" />
</LinearLayout>
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
style="@style/Widget.Neumorph.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/userNameReg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
android:gravity="left"
android:fontFamily="@font/solaimanlipi"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:drawableLeft="@drawable/ic_baseline_call_24"
android:padding="14dp"
android:paddingStart="10dp"
android:maxLength="11"
android:hint="আপনার ইউজার নামটি এখানে লিখুন" />
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
style="@style/Widget.Neumorph.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/userMobileReg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:gravity="left"
android:fontFamily="@font/solaimanlipi"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:drawableLeft="@drawable/ic_baseline_call_24"
android:padding="14dp"
android:paddingStart="10dp"
android:maxLength="11"
android:hint="আপনার মোবাইল নম্বরটি এখানে লিখুন" />
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphCardView
style="@style/Widget.Neumorph.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/userPasswordReg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:gravity="left"
android:fontFamily="@font/solaimanlipi"
android:textColor="@color/black"
android:textColorHint="@color/black"
android:drawableLeft="@drawable/ic_baseline_call_24"
android:padding="14dp"
android:paddingStart="10dp"
android:maxLength="11"
android:hint="আপনার পাসওয়ার্ডটি এখানে লিখুন" />
</soup.neumorphism.NeumorphCardView>
<soup.neumorphism.NeumorphButton
android:id="@+id/btnReg"
style="@style/Widget.Neumorph.Button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:fontFamily="@font/solaimanlipi"
android:text="তৈরী করুন"
android:textColor="@color/white"
app:neumorph_backgroundColor="#279EFA"
android:textSize="16dp"
android:layout_marginBottom="25sp">
</soup.neumorphism.NeumorphButton>
</LinearLayout>
</ScrollView>
</LinearLayout>
Java. Code:
URL_REG = "http://www.yourdomain.com/registration.php";
String myUserName = userNameReg.getText().toString().trim();
String myMobileNo = userMobileReg.getText().toString().trim();
String myPass = userPasswordReg.getText().toString().trim();
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("এ্যকাউন্টটি তৈরী হচ্ছে, দয়াকরে কিছুক্ষণ অপেক্ষা করুন। ধন্যবাদ");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST,URL_REG,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
int success = jsonResponse.getInt("success");
String message = jsonResponse.getString("message");
progressDialog.dismiss();
if (success == 1) {
// Registration successful, display a success message
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
AfterSendText();
} else {
// Registration failed, display an error message
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Error occurred, display an error message
progressDialog.dismiss();
Toast.makeText(getActivity(), "আপনার ইন্টারনেট সংযোগটি হয়তো বিছিন্ন আছে। দয়াকরে আপানর ইন্টারনেট সংযোগটি চেক করুন অথবা কিছুক্ষণ পর আবার চেষ্টা করুন। ধন্যবাদ", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("UserName", myUserName);
params.put("PhoneNo", myMobileNo);
params.put("Password", myPass);
params.put("MobileNo", mPhoneNumber);
return params;
}
};
Volley.newRequestQueue(this).add(stringRequest);
}
Php Code:
<?php
$con = mysqli_connect("localhost","root","password","dbName");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$UserName = $_POST['UserName'];
$PhoneNo = $_POST['PhoneNo'];
$Password = $_POST['Password'];
$MobileNo = $_POST['MobileNo'];
//$hash_password = password_hash($Password, PASSWORD_DEFAULT);
$query = "SELECT * FROM tbl_registration WHERE PhoneNo = '$PhoneNo'";
$result = mysqli_query($con, $query);
if (mysqli_num_rows($result) > 0) {
// User already exists
$response["success"] = 0;
$response["message"] = "এই নাম্বারে আগে রেজিস্ট্রেশন করা আছে।";
} else {
$query = "INSERT INTO tbl_registration(UserName,PhoneNo,Password,MobileNo) VALUES('$UserName','$PhoneNo','$Password','$MobileNo')";
$result = mysqli_query($con, $query);
if ($result) {
// User registered successfully
$response["success"] = 1;
$response["message"] = "নতুন একাউন্ট তৈরী করে আমাদের সাথে থাকার জন্য আমরা আন্তরিকভাবে ধন্যবাদ জানাচ্ছি। এখন আপনি লগইন করতে পারেন";
} else {
// Registration failed
$response["success"] = 0;
$response["message"] = "আমরা আন্তরিকভাবে দুঃখিত এই মূহুর্তে আপনার একাউন্টি তৈরী করা সম্ভব হচ্ছে না, দায়া করে কিছুক্ষণ পর আবার চেষ্টা করুন";
}
}
mysqli_close($con);
echo json_encode($response);
}
?>