Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. package alsaad.layla.ghgf;
  2.  
  3. import android.content.Intent;
  4. import android.os.AsyncTask;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13. import org.apache.http.message.BasicNameValuePair;
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16.  
  17. import java.util.ArrayList;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20. EditText editTextName, editTextPassword;
  21. Button btnLogin;
  22. TextView textViewRegister, textViewForget;
  23.  
  24. String URL= "http://10.0.3.2/GHGF/index.php";
  25.  
  26. JSONParser jsonParser=new JSONParser();
  27.  
  28.  
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34.  
  35. editTextName = (EditText)findViewById(R.id.editTextName);
  36. editTextPassword = (EditText)findViewById(R.id.editTextPassword);
  37. textViewRegister = (TextView)findViewById(R.id.textViewRegister);
  38. textViewForget = (TextView)findViewById(R.id.textViewForget);
  39.  
  40. btnLogin = (Button)findViewById(R.id.btnLogin);
  41. btnLogin.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View view) {
  44.  
  45. AttemptLogin attemptLogin= new AttemptLogin();
  46. attemptLogin.execute(editTextName.getText().toString(),editTextPassword.getText().toString());
  47.  
  48.  
  49. }
  50. });
  51.  
  52. textViewRegister.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View view) {
  55. Intent reg=new Intent(MainActivity.this,Register.class);
  56. startActivity(reg);
  57.  
  58. }
  59. });
  60.  
  61.  
  62.  
  63.  
  64. }
  65.  
  66. public class AttemptLogin extends AsyncTask<String, Void, JSONObject> {
  67.  
  68. @Override
  69.  
  70. protected void onPreExecute() {
  71.  
  72. super.onPreExecute();
  73.  
  74. }
  75.  
  76. @Override
  77.  
  78. protected JSONObject doInBackground(String... args) {
  79.  
  80.  
  81. String password = args[1];
  82. String name= args[0];
  83.  
  84. ArrayList params = new ArrayList();
  85. params.add(new BasicNameValuePair("username", name));
  86. params.add(new BasicNameValuePair("password", password));
  87.  
  88.  
  89. JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params);
  90.  
  91.  
  92. return json;
  93.  
  94. }
  95.  
  96. protected void onPostExecute(JSONObject result) {
  97.  
  98. // dismiss the dialog once product deleted
  99. //Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
  100.  
  101. try {
  102. if (result != null) {
  103. Toast.makeText(getApplicationContext(),result.getString("message"),Toast.LENGTH_LONG).show();
  104.  
  105.  
  106. } else {
  107. Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
  108. }
  109. } catch (JSONException e) {
  110. e.printStackTrace();
  111. }
  112.  
  113.  
  114. }
  115.  
  116. }
  117. }
  118.  
  119. require_once 'user.php';
  120.  
  121. $username = "";
  122.  
  123. $password = "";
  124.  
  125. $email = "";
  126.  
  127. if(isset($_POST['username'])){
  128.  
  129. $username = $_POST['username'];
  130.  
  131. }
  132.  
  133. if(isset($_POST['password'])){
  134.  
  135. $password = $_POST['password'];
  136.  
  137. }
  138.  
  139. if(isset($_POST['email'])){
  140.  
  141. $email = $_POST['email'];
  142.  
  143. }
  144.  
  145. $userObject = new User();
  146.  
  147. // Registration
  148.  
  149. if(!empty($username) && !empty($password) && !empty($email)){
  150.  
  151. $hashed_password = md5($password);
  152.  
  153. $json_registration = $userObject->createNewRegisterUser($username, $hashed_password, $email);
  154.  
  155. echo json_encode($json_registration);
  156.  
  157. }
  158.  
  159. // Login
  160.  
  161. if(!empty($username) && !empty($password) && empty($email)){
  162.  
  163. $hashed_password = md5($password);
  164.  
  165. $json_array = $userObject->loginUsers($username, $hashed_password);
  166.  
  167. echo json_encode($json_array);
  168. }
  169. ?>
  170.  
  171. include_once 'db-connect.php';
  172.  
  173. class User{
  174.  
  175. private $db;
  176.  
  177.  
  178. private $db_table = "donor";
  179.  
  180. public function __construct(){
  181. $this->db = new DbConnect();
  182. }
  183.  
  184. public function isLoginExist($username, $password){
  185.  
  186. $query = "select * from ".$this->db_table." where username = '$username' AND password = '$password' Limit 1";
  187.  
  188. $result = mysqli_query($this->db->getDb(), $query);
  189.  
  190. if(mysqli_num_rows($result) > 0){
  191.  
  192. mysqli_close($this->db->getDb());
  193.  
  194.  
  195. return true;
  196.  
  197. }
  198.  
  199. mysqli_close($this->db->getDb());
  200.  
  201. return false;
  202.  
  203. }
  204. public function isEmailUsernameExist($username, $email){
  205.  
  206. $query = "select * from ".$this->db_table." where username = '$username' AND email = '$email'";
  207.  
  208. $result = mysqli_query($this->db->getDb(), $query);
  209.  
  210. if(mysqli_num_rows($result) > 0){
  211.  
  212. mysqli_close($this->db->getDb());
  213.  
  214. return true;
  215.  
  216. }
  217.  
  218. return false;
  219.  
  220. }
  221.  
  222. public function isValidEmail($email){
  223. return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
  224. }
  225.  
  226. public function createNewRegisterUser($username, $password, $email, $phone, $city){
  227.  
  228. $isExisting = $this->isEmailUsernameExist($username, $email);
  229.  
  230. if($isExisting){
  231.  
  232. $json['success'] = 0;
  233. $json['message'] = "Error in registering. Probably the username/email already exists";
  234. }
  235.  
  236. else{
  237.  
  238. $isValid = $this->isValidEmail($email);
  239.  
  240. if($isValid)
  241. {
  242. $query = "insert into ".$this->db_table." (username, password, email, phone, city) values ('$username', '$password', '$email', '$phone', '$city')";
  243.  
  244. $inserted = mysqli_query($this->db->getDb(), $query);
  245.  
  246. if($inserted == 1){
  247.  
  248. $json['success'] = 1;
  249. $json['message'] = "Successfully registered the user";
  250.  
  251. }else{
  252.  
  253. $json['success'] = 0;
  254. $json['message'] = "Error in registering. Probably the username/email already exists";
  255.  
  256. }
  257.  
  258. mysqli_close($this->db->getDb());
  259. }
  260. else{
  261. $json['success'] = 0;
  262. $json['message'] = "Error in registering. Email Address is not valid";
  263. }
  264.  
  265. }
  266.  
  267. return $json;
  268.  
  269. }
  270.  
  271. public function loginUsers($username, $password){
  272.  
  273. $json = array();
  274.  
  275. $canUserLogin = $this->isLoginExist($username, $password);
  276.  
  277. if($canUserLogin){
  278.  
  279. $json['success'] = 1;
  280. $json['message'] = "Successfully logged in";
  281.  
  282.  
  283. }else{
  284. $json['success'] = 0;
  285. $json['message'] = "Incorrect details";
  286. }
  287. return $json;
  288. }
  289. }
  290. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement