title: Android and PHP login authentication
body: <p>I am trying to make an app on android in which user needs to log in app to use it. log in authentication will be done by PHP web service. I have a <code>login.java</code> class <code>CustomeHTTPClient</code> and this one is an sample code I have got from internet.There is a method in <code>login.java</code> class name <code>connectphp</code> is connecting to web service and using response to display toast message</p>
  
  <p><strong>Login.java</strong></p>
  
  <pre><code>package com.boyzcorn.android.fyp;
  
  import java.util.ArrayList;
  import org.apache.http.NameValuePair;  
  import org.apache.http.message.BasicNameValuePair;  
  
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  
  public class login extends Activity {
  /** Called when the activity is first created. */
       EditText eText ;
      EditText eText2 ;
      Button btnSubmit ;
      Button btnSignup ;
  
      public void validation()
      {
          if(eText.getText().toString().equals("") ||
                  eText.getText().toString().equals(""))
                  {
                  Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
                  }
          else
          {
              connectphp();
          }
          }
  
  
      public void connectphp()
      {
      // TODO Auto-generated method stub
      ArrayList&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();
      postParameters.add(new BasicNameValuePair("username", eText.getText().toString()));
      postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString()));
  Passing Parameter to the php web service for authentication
      //String valid = "1";
      String response = null;
      try {
      response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php", postParameters);  //Enter Your remote PHP,ASP, Servlet file link
      String res=response.toString();
      // res = res.trim();
      res= res.replaceAll("\\s+","");
      //error.setText(res);
      if(res.equals("1"))
      {
          Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
          Intent i = new Intent(login.this,order_pushing.class);
          startActivity(i);
      }
          else
              if(res.equals("0"))
          {
  
          Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
          }
      } catch (Exception e) {
  
      eText.setText(e.toString());
      }}
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.login);
   eText = (EditText)findViewById(R.id.uid);
   eText2 = (EditText)findViewById(R.id.editText2);
  btnSubmit = (Button)findViewById(R.id.sbtn);
   btnSignup = (Button)findViewById(R.id.signupbtn);
  btnSubmit.setOnClickListener(new OnClickListener() {
  
  public void onClick(View v)
  { 
      validation(); (This is to check empty fields)
  
  
  }
  });
  
  btnSignup.setOnClickListener(new View.OnClickListener() {
  
      public void onClick(View arg0) {
      Intent i = new Intent(login.this,signup.class);
      startActivity(i);
      }
   });
  
  }}
  </code></pre>
  
  <p>This is my <code>customhtpclient</code> class in which http connection define.</p>
  
  <pre><code>**CustomHTTPClient.java**
  
  package com.boyzcorn.android.fyp;
  
  import java.util.ArrayList;
  import org.apache.http.NameValuePair;  
  import org.apache.http.message.BasicNameValuePair;  
  
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  
  public class login extends Activity {
  /** Called when the activity is first created. */
       EditText eText ;
      EditText eText2 ;
      Button btnSubmit ;
      Button btnSignup ;
  
      public void validation()
      {
          if(eText.getText().toString().equals("") ||
                  eText.getText().toString().equals(""))
                  {
                  Toast.makeText( getApplicationContext(),"Fill Empty Fields",Toast.LENGTH_SHORT ).show();
                  }
          else
          {
              connectphp();
          }
          }
  
  
      public void connectphp()
      {
      // TODO Auto-generated method stub
      ArrayList&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();
      postParameters.add(new BasicNameValuePair("username", eText.getText().toString()));
      postParameters.add(new BasicNameValuePair("pass1", eText2.getText().toString()));
  
      //String valid = "1";
      String response = null;
      try {
      response = CustomHttpClient.executeHttpPost("http://10.0.2.2:8082/WebService/login.php",postParameters);  //Enter Your remote PHP,ASP, Servlet file link 
      String res=response.toString();
      // res = res.trim();
      res= res.replaceAll("\\s+","");
      //error.setText(res);
      if(res.equals("1"))
      {
          Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
          Intent i = new Intent(login.this,order_pushing.class);
          startActivity(i);
      }
          else
              if(res.equals("0"))//Server response if 0
          {
  
          Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
          }
      } catch (Exception e) {
  
      eText.setText(e.toString());
      }}
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.login);
   eText = (EditText)findViewById(R.id.uid);
   eText2 = (EditText)findViewById(R.id.editText2);
  btnSubmit = (Button)findViewById(R.id.sbtn);
   btnSignup = (Button)findViewById(R.id.signupbtn);
  btnSubmit.setOnClickListener(new OnClickListener() {
  
  public void onClick(View v)
  { 
      validation();
  
  
  }
  });
  
  btnSignup.setOnClickListener(new View.OnClickListener() {
  
      public void onClick(View arg0) {
      Intent i = new Intent(login.this,signup.class);
      startActivity(i);
      }
   });
  
  }}
  </code></pre>
  
  <p>And my php web service name <strong>login.ph</strong></p>
  
  <pre><code>&lt;?php
  include("Config.php");
  
  
  // username and password sent from Form
  </code></pre>
  
  <p>Receiving parameters
      $myusername=addslashes($_POST['username']);</p>
  
  <pre><code>$mypassword=addslashes($_POST['pass1']);
  
  $sql="SELECT * FROM signup WHERE user_id='$myusername' and password='$mypassword'";
  
  $result=mysql_query($sql);
  
  $row=mysql_fetch_array($result);
  
  $active=$row['active'];
  
  $count=mysql_num_rows($result);
  </code></pre>
  
  <p>If result matched $myusername and $mypassword, table row must be 1 row</p>
  
  <pre><code>if($count==1)
      {
  
  echo "1";(If result found send 1 to android)
  
      }
  else
      {
  echo "0";(If result not found send o to android)
  
      }
  
  ?&gt;
  </code></pre>
  
  <p>Config file have all connection parameters to establish connection with database that is mysql and I am using wamp server.<br>
  <strong>*<em>Config.php</em>*</strong></p>
  
  <pre><code>&lt;?php
  
  $mysql_hostname = "localhost";
  
  $mysql_user = "root";
  
  $mysql_password = "";
  
  $mysql_database = "kse";
  
  $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
  
  or
   die("Opps some thing went wrong");
  
  mysql_select_db($mysql_database, $bd) 
  
  or
  
  die("Opps some thing went wrong");
  ?&gt;
  </code></pre>
  
  <p>When I am running my app it is giving me toast messgae if I'm giving wrong log in details as I use if and else here</p>
  
  <blockquote>
  <pre><code>if(res.equals("1"))
    {
        Toast.makeText( getApplicationContext(),"Correct Username or Password",Toast.LENGTH_SHORT ).show();
        Intent i = new Intent(login.this,order_pushing.class);
        startActivity(i);
    }
        else
            if(res.equals("0"))
        {
  
        Toast.makeText( getApplicationContext(),"Sorry!! Incorrect Username or Password",Toast.LENGTH_SHORT ).show();
        }
  </code></pre>
  </blockquote>
  
  <p>But when I am giving correct information, no response..Please help me .I am just a beginner to android.If you could make corrections in my code please.</p>