Guest User

Untitled

a guest
Aug 26th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. Cannot authentication
  2. <?php
  3. include("ConnectDatabase.php");
  4. $Username = $_POST['Username'];
  5. $Password = $_POST['Password'];
  6.  
  7. $q = mysql_query("SELECT Username, Password FROM Users
  8. where Username = '".$Username."' and
  9. Password = '".$Password."'");
  10.  
  11. if(mysql_num_rows($q) > 0){
  12. print json_encode($q);
  13. }else{
  14. print "1";
  15. }
  16. mysql_close();
  17. ?>
  18.  
  19. public class Authentication extends Activity implements OnClickListener,
  20. OnKeyListener, OnCheckedChangeListener {
  21.  
  22. /** Called when the activity is first created. */
  23.  
  24. ArrayList<NameValuePair> authentication;
  25. String passIn, userIn, result;
  26. EditText username, password;
  27. CheckBox remember;
  28. Button b_login;
  29. InputMethodManager inputManager;
  30. InputStream is;
  31.  
  32. @Override
  33. public void onCreate(Bundle savedInstanceState) {
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.main);
  36. // userIn = username.getText().toString();
  37. // passIn = password.getText().toString();
  38. username = (EditText) findViewById(R.id.usrname);
  39. password = (EditText) findViewById(R.id.password);
  40. inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
  41. remember = (CheckBox) findViewById(R.id.remember);
  42. remember.setOnCheckedChangeListener(this);
  43. b_login = (Button) findViewById(R.id.login);
  44. b_login.setOnClickListener(this);
  45. username = (EditText) findViewById(R.id.usrname);
  46. username.setOnKeyListener(this);
  47. password = (EditText) findViewById(R.id.password);
  48. password.setOnKeyListener(this);
  49.  
  50. }
  51.  
  52. public void onClick(View v) {
  53. // TODO Auto-generated method stub
  54. switch (v.getId()) {
  55. case R.id.login:
  56. while (true) {
  57. userIn = username.getText().toString();
  58. passIn = password.getText().toString();
  59. try {
  60. sendAuthenticationData(userIn, passIn);
  61. break;
  62. } catch (ClientProtocolException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. } catch (IOException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. } catch (WrongInputException e) {
  69. Toast.makeText(Authentication.this, "Invalid username or password", Toast.LENGTH_LONG);
  70.  
  71. }
  72. // break;
  73. }
  74. Intent intent = new Intent(this, ApplicationMenus.class);
  75. this.startActivity(intent);
  76. clearText(username,password);
  77.  
  78. break;
  79. }
  80. }
  81.  
  82. public boolean onKey(View v, int keyCode, KeyEvent event) {
  83. // TODO Auto-generated method stub
  84. if ((event.getAction() == KeyEvent.ACTION_DOWN)
  85. && (keyCode == KeyEvent.KEYCODE_ENTER)) {
  86. // Perform action on key press
  87.  
  88. inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
  89. return true;
  90. }
  91. return false;
  92. }
  93.  
  94.  
  95. public void clearText(EditText usr, EditText pass) {
  96. usr.setText("");
  97. pass.setText("");
  98. }
  99.  
  100. public void sendAuthenticationData(String username, String password)
  101. throws ClientProtocolException, IOException, WrongInputException {
  102.  
  103. authentication = new ArrayList<NameValuePair>();
  104. authentication.add(new BasicNameValuePair("Username", userIn));
  105. authentication.add(new BasicNameValuePair("Password", passIn));
  106. this.sendData(authentication);
  107. }
  108.  
  109. public void sendData(ArrayList<NameValuePair> data)
  110. throws ClientProtocolException, IOException, WrongInputException {
  111.  
  112. HttpClient httpclient = new DefaultHttpClient();
  113. HttpPost httppost = new HttpPost(
  114. "path/Authentication.php"); // I use real path here
  115. httppost.setEntity(new UrlEncodedFormEntity(data));
  116. HttpResponse response = httpclient.execute(httppost);
  117. HttpEntity entity = response.getEntity();
  118. // is = entity.getContent();
  119. String temp = EntityUtils.toString(entity);
  120.  
  121. if (temp.equals("1")) {
  122. throw new WrongInputException();
  123. }
  124.  
  125. }}
  126.  
  127. $Username = $_POST['Username'];
  128.  
  129. if (mysql_num_rows($q) > 0) {
  130. print json_encode($q);
  131.  
  132. print json_encode(mysql_fetch_assoc($q));
Add Comment
Please, Sign In to add comment