0

I need to make andoird app for user login authentication.

This is my php code :

<?php
     $con = mysql_connect('ip','root','pass') or die('cannot connect to db');
     mysql_select_db('master1') or die('cannot select db');
     if(isset($_POST['email']) && isset($_POST['pass'])) {      
          $email = $_POST['email'];
          $pass = $_POST['pass'];
          $query = "SELECT * FROM Users WHERE user_email='$email' AND user_password='$pass'";    
          $res=mysql_query($query);
     }

     if(mysql_num_rows($res) > 0) 
    echo "You are successfully logged in";
   else 
   echo "Incorrect email or password";

And this is my android code:

    @Override
    protected String doInBackground(String... params) {
        InputStream is1 = null;
        for(String url1:params) {
            try {
                ArrayList<NameValuePair> pairs = new ArrayList<>();
                pairs.add(new BasicNameValuePair("email", etEmail.getText().toString()));
                pairs.add(new BasicNameValuePair("pass", etPass.getText().toString()));
                HttpClient client = new DefaultHttpClient();
                HttpPost post = new HttpPost(url1);
                post.setEntity(new UrlEncodedFormEntity(pairs));
                HttpResponse response = client.execute(post);
                is1 = response.getEntity().getContent();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(is1, "UTF-8"), 8);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
            String line = null;
            try {
                while ((line=reader.readLine())!=null) {
                    text += line + "\n";
                }
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
            try {
                is1.close();
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(LoginActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
            }
        }
            return text;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Toast.makeText(LoginActivity.this, s, Toast.LENGTH_SHORT).show();
        if (s.contains("You are successfully logged in")){
            Intent i = new Intent(LoginActivity.this,ConnectActivity.class);
            i.putExtra("user_email",etEmail.getText().toString());
            startActivity(i);
        }
        dialog.dismiss();
    }
}

The problem is that I get whole php script as a return result in my app, and I need just php echo, which can have two possible values, as you can see in my code, depending on the values that user inserts.

Does anyone know what could be the problem in my code, is it in php part or in android part ???

MarkoDj
  • 1
  • 3
  • Are you sure you have a web server serving the php file? Sounds like you're just reading the contents of the php file. – jeoj Sep 15 '15 at 14:00
  • Sounds like your webserver isn't making an attempt to parse the PHP. See if [this question](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page) helps you. – HPierce Sep 15 '15 at 14:36
  • You have to use a json encode instead of echo and use a string builder and stream reader in android. – Subin Thomas Sep 15 '15 at 15:18
  • I think the problem was with web server, when I fiixed that app worked properly – MarkoDj Sep 15 '15 at 20:40

1 Answers1

0

The problem is with your echo statement, you cannot print data while accessing the PHP script from Android instead you can make a response array to send data to the Android app like this:

if(mysql_num_rows($res) > 0) {

  $response["success"] = 1;
  $response["message"] = "You are successfully logged in";
  echo json_encode($response);
}
else{
  $response["success"] = 0;
  $response["message"] = "Incorrect email or password";
  echo json_encode($response);
}

You should refer to this link: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Ashish Shukla
  • 1,027
  • 16
  • 36
  • I went through the tutorial in your link but I found that way a litlle bit more difficult so I wrote the code as could see in my question and when I fixed web server I got desirable result. I am not sure really, I am still beginner, I guess your way is better, but for now this mine is doing the job too. Thank you anyway for advice. – MarkoDj Sep 15 '15 at 20:46
  • the above link will help you when you are working with JSON and fetching a lot of data from server and sending data to server may be in the future. – Ashish Shukla Sep 16 '15 at 07:23