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 ???