Simple Login Example with Php

Login.php


For Php
Do not use == for string comparison. === is better.


https://mdad.000webhostapp.com/Login.php




In this simple Login Example I use name and email field for Username and Password

Login.php
<?php
$name = "a";    //for testing with fixed data
$email = "a";     //for testing with fixed data

$login = "false";

$conn=mysqli_connect("localhost","id4388354_mdaduser","mdad2301","id4388354_mdad");


$sql ="select * from  Example";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
 //fetch data from mysql and put into $row
 while($row = $result->fetch_assoc())
 {
              
  if ( ($row["name"] === $name) && ($row["email"] === $email))
                {
                      
                      $login = "success";
                     break;
                      
                 }

 
 }
}
else
{
 echo "0 results";
}
$conn->close();

echo ($login );

 

?>

Test it with Login1.html

https://mdad.000webhostapp.com/Login1.html


Login1.html


<form action="Login1.php" method="post">
 
  <table>

  <tr>
    <td><label><b>Username</b></label></td>
     <td><input type="text" placeholder="Enter Username" name="name" required></td>
  </tr>
   <tr>
    <td><label><b>Password</b></label></td>
    <td><input type="password" placeholder="Enter Password" name="email" required></td>
  </tr>
       <tr><td></td><td><button type="submit">Login</button> </td> </tr>
   
  </table>

 
</form>


My database table : Example
nameageemail
Jacky22jacky@gmail.com
Alice21alice@wonderland.com
john33john@walker.com
sw13qad
www11ee
1111hkg
we8868vhgigp
aaa


Login1.php

<?php

//$name = "a";  //For Testing
//$email = "a";  //For Testing


$name = $_POST['name'];
$email = $_POST['email'];



$login = "false";

$conn=mysqli_connect("localhost","id4388354_mdaduser","mdad2301","id4388354_mdad");


$sql ="select * from  Example";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
 //fetch data from mysql and put into $row
 while($row = $result->fetch_assoc())
 {
              
  if ( ($row["name"] === $name) && ($row["email"] === $email))
                {
                      
                      $login = "success";
                     break;
                      
                 }

 
 }
}
else
{
 echo "0 results";
}
$conn->close();

echo ($login );

//https://mdad.000webhostapp.com/
//https://mdad18.000webhostapp.com/

?>

class loginA extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... arg0) {
   // TODO Auto-generated method stub
   
 
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://mdad.000webhostapp.com/Login1.php");
 
httppost.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


  nameValuePairs.add(new BasicNameValuePair("name", name));
   nameValuePairs.add(new BasicNameValuePair("email", email));  //email like password for login

 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 HttpResponse responsePOST = httpclient.execute(httppost);


  System.out.println(nameValuePairs);
 
HttpEntity resEntity = responsePOST.getEntity(); 
String response=EntityUtils.toString(resEntity); // content will be consume only once

Log.e("XXX",response);

if (response.equalsIgnoreCase("Success"))
{
 System.out.println("Login Successfully");
}
else

{
 System.out.println("Invalid Login!");
 
}
//HealthDAO.getInstance(frame).updateCompare(Cid, "Updated");
} catch (Exception e) {
System.out.println("Error " + e);
}

   return null;
  }
 }


Login from Android (Partial Codes) Guide only

package com.example.getphonedbexample;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

 EditText etName, etAge, etEmail;
 Button bSubmit;
 String name = "";
 String age = "";
 String email = ""; 
 @SuppressLint("NewApi") 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  //Setup the strictmode policy
 // StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 // StrictMode.setThreadPolicy(policy);
  
 
  etName = (EditText) findViewById(R.id.editName);
  etAge = (EditText) findViewById(R.id.editAge);
   etEmail = (EditText) findViewById(R.id.editEmail);  
   bSubmit = (Button) findViewById(R.id.submitBtn);
 
  bSubmit.setOnClickListener (new View.OnClickListener() {
   InputStream is = null;
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    //Storing the values inside the editTexts inside the string variables
     name = ""+etName.getText().toString();
     age = ""+etAge.getText().toString();
     email = ""+etEmail.getText().toString();
     new loginA().execute("");

   }
   
  });
  
  
  
  
 }
  
 
 
 class loginA extends AsyncTask<String, String, String> {

  @Override
  protected String doInBackground(String... arg0) {
   // TODO Auto-generated method stub
   
 
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://mdad.000webhostapp.com/Login1.php");
 
httppost.setHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


  nameValuePairs.add(new BasicNameValuePair("name", name));
   nameValuePairs.add(new BasicNameValuePair("email", email));  //email like password for login

 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 HttpResponse responsePOST = httpclient.execute(httppost);


  System.out.println(nameValuePairs);
 
HttpEntity resEntity = responsePOST.getEntity(); 
String response=EntityUtils.toString(resEntity); // content will be consume only once

Log.e("XXX",response);

if (response.equalsIgnoreCase("Success"))
{
 System.out.println("Login Successfully");
 return "Success";
} else { System.out.println("Invalid Login!"); } //HealthDAO.getInstance(frame).updateCompare(Cid, "Updated"); } catch (Exception e) { System.out.println("Error " + e); } return "Invalid"; } protected void onPostExecute(String result) { //if (result.equalsIgnoreCase("Success")) //{ //Intent intent = new Intent (); // intent.startActivity //} //else //{ // Toast.............. Invalid Logn //} }//end onPostExecute }//end of LoginA inner class }