Hey guys! I'm making an android login page through php and mysql. the php and mysql are working fine, but when I send user information from app to php so as to checking them, the echo turns back as a html page. as bellow:
<html>
<body>
<script type="text/javascript" src="/aes.js"></script>
<script>
function toNumbers(d) {
var e = [];
d.replace(/(..)/g, function(d) {
e.push(parseInt(d,16))
});
return e
}
function toHex() {
for (var d = [], d = 1 == arguments.length
&& arguments[0].constructor == Array ? arguments[0] : arguments, e = "",
f = 0; f < d.length; f++) {
e += (16 > d[f] ? "0" : "") + d[f].toString(16);
}
return e.toLowerCase()
}
var a = toNumbers("f655ba9d09a112d4968c63579db590b4"),
b = toNumbers("98344c2eee86c3994890592585b49f80"),
c=toNumbers("fb8ba46111eda80cc729391c1afb0580");
document.cookie = "__test=" + toHex(slowAES.decrypt(c, 2, a, b)) + "; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/";
location.href = "http://myloginpage.webpi.ir/login.php?i=1";
</script>
<noscript>This site requires Javascript to work.</noscript>
</body>
</html>
Here is my PHP code:
<?php
$servername = "******";
$username = "******";
$password = "******";
$dbname = "******";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
$username = $_POST['username'];
$password = $_POST['password'];
$query = mysqli_query($con, "SELECT * FROM tbl_user WHERE username='$username' and password='$password'");
if (mysqli_num_rows($query) > 0) {
echo "success";
} else {
echo "failure";
}
mysqli_close($con);
?>
my MainActivity code: note!(I have tried with both apache.http and httpconnection and both got the same result)
public class MainActivity extends ActionBarActivity {
private EditText editTextUserName;
private EditText editTextPassword;
public static final String USER_NAME = "USERNAME";
String username;
String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUserName = (EditText) findViewById(R.id.editTextUserName);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
}
public void invokeLogin(View view){
username = editTextUserName.getText().toString();
password = editTextPassword.getText().toString();
login(username,password);
}
private void login(final String username, String password) {
class LoginAsync extends AsyncTask<String, Void, String>{
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
String uname = params[0];
String pass = params[1];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", uname));
nameValuePairs.add(new BasicNameValuePair("password", pass));
String result = null;
try{
HttpClient httpClient = new DefaultHttpClient();
// httpClient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
HttpPost httpPost = new HttpPost(
"http://myloginpage.webpi.ir/login.php/");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "
");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result){
String s = result.trim();
editTextUserName.setText(s);
loadingDialog.dismiss();
if(s.equalsIgnoreCase("success")){
Intent intent = new Intent(MainActivity.this, UserProfile.class);
intent.putExtra(USER_NAME, username);
finish();
startActivity(intent);
}else {
Toast.makeText(getApplicationContext(), "Invalid User Name or Password", Toast.LENGTH_LONG).show();
}
}
}
LoginAsync la = new LoginAsync();
la.execute(username, password);
}
}