Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package alsaad.layla.ghgf;
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.apache.http.message.BasicNameValuePair;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- public class MainActivity extends AppCompatActivity {
- EditText editTextName, editTextPassword;
- Button btnLogin;
- TextView textViewRegister, textViewForget;
- String URL= "http://10.0.3.2/GHGF/index.php";
- JSONParser jsonParser=new JSONParser();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- editTextName = (EditText)findViewById(R.id.editTextName);
- editTextPassword = (EditText)findViewById(R.id.editTextPassword);
- textViewRegister = (TextView)findViewById(R.id.textViewRegister);
- textViewForget = (TextView)findViewById(R.id.textViewForget);
- btnLogin = (Button)findViewById(R.id.btnLogin);
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- AttemptLogin attemptLogin= new AttemptLogin();
- attemptLogin.execute(editTextName.getText().toString(),editTextPassword.getText().toString());
- }
- });
- textViewRegister.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent reg=new Intent(MainActivity.this,Register.class);
- startActivity(reg);
- }
- });
- }
- public class AttemptLogin extends AsyncTask<String, Void, JSONObject> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- @Override
- protected JSONObject doInBackground(String... args) {
- String password = args[1];
- String name= args[0];
- ArrayList params = new ArrayList();
- params.add(new BasicNameValuePair("username", name));
- params.add(new BasicNameValuePair("password", password));
- JSONObject json = jsonParser.makeHttpRequest(URL, "POST", params);
- return json;
- }
- protected void onPostExecute(JSONObject result) {
- // dismiss the dialog once product deleted
- //Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
- try {
- if (result != null) {
- Toast.makeText(getApplicationContext(),result.getString("message"),Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(getApplicationContext(), "Unable to retrieve any data from server", Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- }
- }
- require_once 'user.php';
- $username = "";
- $password = "";
- $email = "";
- if(isset($_POST['username'])){
- $username = $_POST['username'];
- }
- if(isset($_POST['password'])){
- $password = $_POST['password'];
- }
- if(isset($_POST['email'])){
- $email = $_POST['email'];
- }
- $userObject = new User();
- // Registration
- if(!empty($username) && !empty($password) && !empty($email)){
- $hashed_password = md5($password);
- $json_registration = $userObject->createNewRegisterUser($username, $hashed_password, $email);
- echo json_encode($json_registration);
- }
- // Login
- if(!empty($username) && !empty($password) && empty($email)){
- $hashed_password = md5($password);
- $json_array = $userObject->loginUsers($username, $hashed_password);
- echo json_encode($json_array);
- }
- ?>
- include_once 'db-connect.php';
- class User{
- private $db;
- private $db_table = "donor";
- public function __construct(){
- $this->db = new DbConnect();
- }
- public function isLoginExist($username, $password){
- $query = "select * from ".$this->db_table." where username = '$username' AND password = '$password' Limit 1";
- $result = mysqli_query($this->db->getDb(), $query);
- if(mysqli_num_rows($result) > 0){
- mysqli_close($this->db->getDb());
- return true;
- }
- mysqli_close($this->db->getDb());
- return false;
- }
- public function isEmailUsernameExist($username, $email){
- $query = "select * from ".$this->db_table." where username = '$username' AND email = '$email'";
- $result = mysqli_query($this->db->getDb(), $query);
- if(mysqli_num_rows($result) > 0){
- mysqli_close($this->db->getDb());
- return true;
- }
- return false;
- }
- public function isValidEmail($email){
- return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
- }
- public function createNewRegisterUser($username, $password, $email, $phone, $city){
- $isExisting = $this->isEmailUsernameExist($username, $email);
- if($isExisting){
- $json['success'] = 0;
- $json['message'] = "Error in registering. Probably the username/email already exists";
- }
- else{
- $isValid = $this->isValidEmail($email);
- if($isValid)
- {
- $query = "insert into ".$this->db_table." (username, password, email, phone, city) values ('$username', '$password', '$email', '$phone', '$city')";
- $inserted = mysqli_query($this->db->getDb(), $query);
- if($inserted == 1){
- $json['success'] = 1;
- $json['message'] = "Successfully registered the user";
- }else{
- $json['success'] = 0;
- $json['message'] = "Error in registering. Probably the username/email already exists";
- }
- mysqli_close($this->db->getDb());
- }
- else{
- $json['success'] = 0;
- $json['message'] = "Error in registering. Email Address is not valid";
- }
- }
- return $json;
- }
- public function loginUsers($username, $password){
- $json = array();
- $canUserLogin = $this->isLoginExist($username, $password);
- if($canUserLogin){
- $json['success'] = 1;
- $json['message'] = "Successfully logged in";
- }else{
- $json['success'] = 0;
- $json['message'] = "Incorrect details";
- }
- return $json;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement