Android - 登入系統:忘記密碼查詢


簡單實做忘記密碼的查詢
假設需要名字、信箱、跟電話三種資訊才能完成查詢
Android端
  1. public class forgotpw extends AppCompatActivity {
  2. private EditText editTextName;
  3. private EditText editTextEmail;
  4. private EditText editTextTel;
  5. private Button buttonget;
  6. private static final String REGISTER_URL = "http://網址/forgot.php";
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_forgotpw);
  11.  
  12. editTextName = (EditText) findViewById(R.id.name);
  13. editTextEmail = (EditText) findViewById(R.id.email);
  14. editTextTel= (EditText) findViewById(R.id.tel);
  15. buttonget = (Button) findViewById(R.id.get);
  16. buttonget.setOnClickListener(buttonListener);
  17. }
  18. private Button.OnClickListener buttonListener = new Button.OnClickListener() {/**監聽查詢鈕是否被按下**/
  19. @Override
  20. public void onClick(View v) {
  21. if(v == buttonget){
  22. getpw();/**呼叫這函式進行使用者資料獲取**/
  23. }
  24. }
  25. };
  26. private void getpw() {/**讀取使用者輸入數據**/
  27. String name = editTextName.getText().toString().trim().toLowerCase();
  28. String email = editTextEmail.getText().toString().trim().toLowerCase();
  29. String tel = editTextTel.getText().toString().trim().toLowerCase();
  30. get(name,email,tel);/**獲取資料成功後,開始進行傳送**/
  31. }
  32. private void get(String name,String email, String tel) {
  33. class RegisterUser extends AsyncTask<String, Void, String> {
  34. Createmem ruc = new Createmem();/**使用Creatmem.class的功能(傳送用的httpclass)**/
  35. @Override
  36. protected void onPreExecute()
  37. {
  38. super.onPreExecute();/**當按下查詢鈕,出現提式窗**/
  39. }
  40. @Override
  41. protected void onPostExecute(String s) {
  42. super.onPostExecute(s);
  43. Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
  44. }
  45. @Override
  46. protected String doInBackground(String... params)/**將資料放入hashmap**/
  47. {
  48. HashMap<String, String> data = new HashMap<String,String>();
  49. data.put("name",params[0]);
  50. data.put("email",params[1]);
  51. data.put("tel",params[2]);
  52. String result = ruc.sendPostRequest(REGISTER_URL,data);
  53. return result;
  54. }
  55. }
  56. RegisterUser ru = new RegisterUser();/**傳送資料**/
  57. ru.execute(name,email, tel);
  58. }
  59. }
Android介面布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/login_bg"
  7. tools:context="project.rmotex.achat.forgotpw">
  8.  
  9. <ImageView android:id="@+id/image"
  10. android:background="@drawable/forgot"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="60dp"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerHorizontal="true" />
  16.  
  17. <LinearLayout
  18. android:orientation="vertical"
  19. android:id="@+id/input"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_marginLeft="28.0dip"
  23. android:layout_marginRight="28.0dip"
  24. android:paddingTop="12.0dip"
  25. android:layout_below="@+id/image">
  26.  
  27. <EditText android:textSize="16.0sp"
  28. android:textColor="#E0E0E0"
  29. android:textColorHint="#E0E0E0"
  30. android:id="@+id/name"
  31. android:background="#40000000"
  32. android:paddingLeft="12.0dip"
  33. android:layout_width="fill_parent"
  34. android:layout_height="44dp"
  35. android:maxLines="1"
  36. android:maxLength="20"
  37. android:hint="Account"
  38. android:inputType="text"/>
  39.  
  40. <View android:background="#ffc0c3c4"
  41. android:layout_width="fill_parent"
  42. android:layout_height="1.0px"
  43. android:layout_marginLeft="1.0px"
  44. android:layout_marginRight="1.0px" />
  45.  
  46. <EditText android:textSize="16.0sp"
  47. android:textColor="#E0E0E0"
  48. android:textColorHint="#E0E0E0"
  49. android:hint="Email"
  50. android:gravity="center_vertical"
  51. android:id="@+id/email"
  52. android:background="#40000000"
  53. android:paddingLeft="12.0dip"
  54. android:layout_width="fill_parent"
  55. android:layout_height="44dp"
  56. android:maxLines="1"
  57. android:maxLength="30"
  58. android:inputType="textEmailAddress" />
  59.  
  60. <View android:background="#ffc0c3c4"
  61. android:layout_width="fill_parent"
  62. android:layout_height="1.0px"
  63. android:layout_marginLeft="1.0px"
  64. android:layout_marginRight="1.0px" />
  65.  
  66. <EditText android:textSize="16.0sp"
  67. android:textColor="#E0E0E0"
  68. android:textColorHint="#E0E0E0"
  69. android:hint="Telephone"
  70. android:gravity="center_vertical"
  71. android:id="@+id/tel"
  72. android:background="#40000000"
  73. android:paddingLeft="12.0dip"
  74. android:layout_width="fill_parent"
  75. android:layout_height="44dp"
  76. android:maxLines="1"
  77. android:maxLength="10"
  78. android:inputType="phone" />
  79.  
  80. <Button
  81. android:layout_width="match_parent"
  82. android:layout_height="match_parent"
  83. android:gravity="center"
  84. android:background="#40000000"
  85. android:text="Get Back"
  86. android:textStyle="bold"
  87. android:id="@+id/get"
  88. android:paddingTop="5.0dip"
  89. android:layout_marginLeft="12.0dip"
  90. android:layout_marginTop="12.0dip"
  91. android:layout_marginRight="12.0dip"
  92. android:textSize="20.0sp"
  93. />
  94. </LinearLayout>
  95. </RelativeLayout>
  96.  
Server端忘記密碼PHP
  1. <?php
  2. /**用於使用者查詢忘記的密碼**/
  3. if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST
  4. $name = $_POST['name'];//將使用者傳的資料存進變數
  5. $email = $_POST['email'];
  6. $tel = $_POST['tel'];
  7. if($name == '' || $email == '' || $tel == ''){
  8. echo '請填滿所有選項!';
  9. }
  10. else
  11. {
  12. require_once('dbConnect.php');
  13. $sql = "SELECT * FROM userinfo WHERE name='$name' AND email='$email' AND tel='$tel'";
  14. $check = mysqli_fetch_array(mysqli_query($con,$sql));
  15. if(isset($check))
  16. {
  17. $sqll ="SELECT password FROM userinfo WHERE name='$name' AND email='$email' AND tel='$tel'";
  18. $result = $con->query($sqll);
  19. if($result->num_rows >0)
  20. {
  21. while($row = $result->fetch_assoc())
  22. {
  23. echo $row['password'];
  24. }
  25. }
  26. }
  27. else
  28. {
  29. echo '請再次檢查輸入資訊!';
  30. }
  31. mysqli_close($con);
  32. }
  33. }
  34. else
  35. {
  36. echo 'Error';
  37. }
  38. ?>

留言