Guest User

Untitled

a guest
May 2nd, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. <?php
  2. header("Content-type: application/json; charset=utf-8");
  3. require_once("dbconnect.php");
  4. require_once 'jdf.php';
  5.  
  6. if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
  7. throw new Exception('Request must be in POST method!');
  8. }
  9. $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
  10. if(strcasecmp($contentType, 'application/json') != 0){
  11. throw new Exception('Request must send in json format!');
  12. }
  13. $content = trim(file_get_contents("php://input"));
  14. $decoded = json_decode($content, true);
  15. if(!is_array($decoded)){
  16. throw new Exception('invalid request!');
  17. }
  18. $res = $decoded;
  19. $data = array();
  20.  
  21. if(array_key_exists('username',$res)){
  22. //username exist
  23. $username = $res['username'];
  24. $pass = md5($res['password']);
  25. $mobile = $res['mobile'];
  26. $stmt = $db->prepare("SELECT * FROM users WHERE username = :a");
  27. $stmt->bindparam(':a',$username);
  28. $stmt->execute();
  29. $c = $stmt->rowCount();
  30. if($c == 0){
  31. $date = jdate("Y/m/d H:i",'','','','en');
  32. $stmt = $db->prepare("INSERT INTO users (mobile, username, password, reg_date)VALUES(:a, :b, :c, :d)");
  33. $stmt->bindparam(':a',$mobile);
  34. $stmt->bindparam(':b',$username);
  35. $stmt->bindparam(':c',$pass);
  36. $stmt->bindparam(':d',$date);
  37. $stmt->execute();
  38. $s = false;
  39. $m = 'register successful!';
  40. }else{
  41. $s = true;
  42. $m = 'this user already registered!';
  43. $res_u = $stmt->fetch();
  44. $username = $res_u['username'];
  45. $date = $res_u['reg_date'];
  46. $data = array('username' => $username, 'date' => $date);
  47. }
  48. }else{
  49. $s = true;
  50. $m = 'username is not sent!';
  51. }
  52.  
  53. $back = array('error' => $s, 'message' => $m, 'data' => $data);
  54. $json_response = json_encode($back);
  55. echo $json_response;
  56.  
  57. package ir.sample.project;
  58.  
  59. import android.content.Intent;
  60. import android.os.Bundle;
  61. import android.support.v7.app.AppCompatActivity;
  62. import android.text.TextUtils;
  63. import android.view.View;
  64. import android.widget.EditText;
  65. import android.widget.ProgressBar;
  66. import android.widget.Toast;
  67.  
  68. import com.android.volley.AuthFailureError;
  69. import com.android.volley.Request;
  70. import com.android.volley.Response;
  71. import com.android.volley.VolleyError;
  72. import com.android.volley.toolbox.StringRequest;
  73.  
  74. import org.json.JSONException;
  75. import org.json.JSONObject;
  76.  
  77. import java.util.HashMap;
  78. import java.util.Map;
  79.  
  80. public class activity_reg extends AppCompatActivity {
  81. EditText txtUser, txtMobile, txtPass;
  82. ProgressBar progressBar;
  83. @Override
  84. protected void onCreate(Bundle savedInstanceState) {
  85. super.onCreate(savedInstanceState);
  86. setContentView(R.layout.activity_reg);
  87.  
  88. progressBar = findViewById(R.id.progressBar);
  89.  
  90. //if the user is already logged in we will directly start the profile activity
  91. if (SharedPrefManager.getInstance(this).isLoggedIn()) {
  92. finish();
  93. startActivity(new Intent(this, activity_dashboard.class));
  94. return;
  95. }
  96.  
  97. txtUser = findViewById(R.id.txt_user);
  98. txtMobile = findViewById(R.id.txt_mobile);
  99. txtPass = findViewById(R.id.txt_pass);
  100.  
  101. findViewById(R.id.btn_reg).setOnClickListener(new View.OnClickListener() {
  102. @Override
  103. public void onClick(View v) {
  104. registerUser();
  105. }
  106. });
  107.  
  108. findViewById(R.id.btn_open_log).setOnClickListener(new View.OnClickListener() {
  109. @Override
  110. public void onClick(View v) {
  111. finish();
  112. startActivity(new Intent(activity_reg.this, activity_login.class));
  113. }
  114. });
  115. }
  116.  
  117. private void registerUser() {
  118. final String username = txtUser.getText().toString().trim();
  119. final String mobile = txtMobile.getText().toString().trim();
  120. final String password = txtPass.getText().toString().trim();
  121. progressBar.setVisibility(View.VISIBLE);
  122. if(TextUtils.isEmpty(username)){
  123. txtUser.setError("Enter username!");
  124. txtUser.requestFocus();
  125. }
  126. if(TextUtils.isEmpty(mobile)){
  127. txtMobile.setError("Enter mobile!");
  128. txtMobile.requestFocus();
  129. }
  130. if(TextUtils.isEmpty(password)){
  131. txtPass.setError("Enter password!");
  132. txtPass.requestFocus();
  133. }
  134.  
  135. StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_REGISTER, new Response.Listener<String>() {
  136. @Override
  137. public void onResponse(String response) {
  138. progressBar.setVisibility(View.GONE);
  139.  
  140. try {
  141. //converting response to json object
  142. JSONObject obj = new JSONObject(response);
  143.  
  144. //if no error in response
  145. if (!obj.getBoolean("error")) {
  146. Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
  147.  
  148. //getting the user from the response
  149. JSONObject userJson = obj.getJSONObject("user");
  150.  
  151. //creating a new user object
  152. User user = new User(
  153. userJson.getInt("id"),
  154. userJson.getString("username"),
  155. userJson.getString("mobile")
  156. );
  157.  
  158. //storing the user in shared preferences
  159. SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);
  160.  
  161. //starting the profile activity
  162. finish();
  163. startActivity(new Intent(getApplicationContext(), activity_dashboard.class));
  164. } else {
  165. Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
  166. }
  167. } catch (JSONException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. }, new Response.ErrorListener() {
  172. @Override
  173. public void onErrorResponse(VolleyError error) {
  174. Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
  175. }
  176. }){
  177. @Override
  178. protected Map<String, String> getParams() throws AuthFailureError {
  179. Map<String, String> params = new HashMap<>();
  180. params.put("username", username);
  181. params.put("mobile",mobile);
  182. params.put("password",password);
  183. return params;
  184. }
  185. };
  186.  
  187. VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
  188.  
  189. }
  190.  
  191.  
  192. }
Add Comment
Please, Sign In to add comment