用户登录,注册,退出等常用功能统一处理 及完成

编辑: admin 分类: 电脑知识 发布时间: 2023-06-14 来源:互联网
用户登录,注册,退出等常用功能统一处理 及完成用户注册功能用户登录,注册,退出等常用功能统一处理

userHandle.php文件代码

  1. <?php
  2. session_start();
  3. require __DIR__ . '/../config/common.php';
  4. $users = require DATA_PATH . '/users.php';
  5. // 获取对用户的操作动作
  6. $action = strtolower($_GET['action']);
  7. $prompt = false;
  8. // 操作白名单
  9. $allowOpts = ['login', 'register', 'logout'];
  10. // 检测操作类型是否合法?
  11. if (!in_array($action, $allowOpts)) {
  12. echo <<< TIPS
  13. <script>
  14. alert('操作类型非法');
  15. location.href='../login.php';
  16. </script>
  17. TIPS;
  18. die;
  19. }
  20. switch ($action):
  21. case 'login':
  22. $json = file_get_contents('php://input');
  23. $user = json_decode($json, true);
  24. $email = $user['email'];
  25. $salt = 'wwwphpcn';
  26. $password = md5($user['password'] . $salt);
  27. $result = array_filter($users, function ($user) use ($email, $password) {
  28. return $user['email'] === $email && $user['password'] === $password;
  29. });
  30. $result = array_values($result);
  31. if (count($result) === 1) {
  32. $prompt = true;
  33. $_SESSION['user']['email'] = $result[0]['email'];
  34. $_SESSION['user']['name'] = $result[0]['name'];
  35. }
  36. break;
  37. case 'logout':
  38. if (session_destroy()) {
  39. $prompt = true;
  40. }
  41. break;
  42. case 'register':
  43. $oriCount = count($users);
  44. $json = file_get_contents('php://input');
  45. $user = json_decode($json, true);
  46. $user['password'] = md5($user['password']);
  47. $user['id'] = count($users) + 1;
  48. $users[] = $user;
  49. if (count($users) === $oriCount + 1) {
  50. $prompt = true;
  51. }
  52. break;
  53. endswitch;
  54. echo json_encode($prompt);
完成用户注册功能

register.php 文件

  1. <?php
  2. // 公共页眉
  3. require __DIR__ . '/template/public/header.php';
  4. ?>
  5. <!-- 主体 -->
  6. <main>
  7. <!-- 用户注册 -->
  8. <form class="login" id="register">
  9. <table>
  10. <caption>
  11. 用户注册
  12. </caption>
  13. <tbody>
  14. <tr>
  15. <td><label for=" nickname">昵称:</label></td>
  16. <td><input type="text" name="nickname" id="nickname" autofocus /></td>
  17. </tr>
  18. <tr>
  19. <td><label for="email">邮箱:</label></td>
  20. <td><input type="email" name="email" id="email" /></td>
  21. </tr>
  22. <tr>
  23. <td><label for="password">密码:</label></td>
  24. <td><input type="password" name="password" id="password" /></td>
  25. </tr>
  26. <tr>
  27. <td><label for="rePassword">确认:</label></td>
  28. <td><input type="password" name="rePassword" id="rePassword" /></td>
  29. </tr>
  30. <tr>
  31. <td colspan="2"><button type="button" onclick="addUser(this)">提交</button></td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. </form>
  36. <p>
  37. <a href="login.php">已有帐号,请直接登录</a>
  38. </p>
  39. </main>
  40. <!-- 页脚 -->
  41. <?php
  42. // 加载外部文件
  43. include 'template/public/footer.php';
  44. ?>
  45. <script>
  46. function addUser(btn) {
  47. // 1. 获取用户的输入
  48. const user = getInput(btn.form);
  49. // 2. 非空验证
  50. if (isEmpty(user)) {
  51. // 3. 验证二次密码是否相等?
  52. if (isPswEqu(user)) {
  53. // 4. 创建提交的数据JSON
  54. const data = createData(user);
  55. // 5. 异步提交
  56. insertUser(data);
  57. }
  58. }
  59. }
  60. // 1. 获取用户输入
  61. const getInput = (form) => {
  62. return {
  63. // 呢称
  64. nickname: {
  65. // dom元素
  66. ele: form.nickname,
  67. // 值
  68. value: form.nickname.value.trim()
  69. },
  70. // 邮箱
  71. email: {
  72. ele: form.email,
  73. value: form.email.value.trim()
  74. },
  75. // 密码
  76. password: {
  77. ele: form.password,
  78. value: form.password.value.trim()
  79. },
  80. // 重复密码
  81. rePassword: {
  82. ele: form.rePassword,
  83. value: form.rePassword.value.trim()
  84. },
  85. }
  86. }
  87. // 2. 非空验证
  88. const isEmpty = (user) => {
  89. switch (true) {
  90. case user.nickname.value.length === 0:
  91. alert('呢称不能为空');
  92. user.nickname.ele.focus();
  93. return false;
  94. case user.email.value.length === 0:
  95. alert('邮箱不能为空');
  96. user.email.ele.focus();
  97. return false;
  98. case user.password.value.length === 0:
  99. alert('密码不能为空');
  100. user.password.ele.focus();
  101. return false;
  102. case user.rePassword.value.length === 0:
  103. alert('重复密码不能为空');
  104. user.rePassword.ele.focus();
  105. return false;
  106. default:
  107. return true;
  108. }
  109. }
  110. // 3. 验证二次密码是否相等?
  111. const isPswEqu = (user) => {
  112. if (user.password.value !== user.rePassword.value) {
  113. alert('二次密码不相等,重新输入');
  114. user.password.ele.focus();
  115. return false;
  116. } else {
  117. return true;
  118. }
  119. }
  120. // 4. 创建提交的数据JSON
  121. const createData = (user) => {
  122. return {
  123. nickname: user.nickname.value,
  124. email: user.email.value,
  125. password: user.password.value,
  126. }
  127. }
  128. // 5. 异步提交
  129. async function insertUser(data) {
  130. console.log(data);
  131. const url = './lib/userHandle.php?action=register';
  132. const response = await fetch(url, {
  133. method: 'POST',
  134. headers: {
  135. 'content-type': 'application/json;charset=utf-8'
  136. },
  137. body: JSON.stringify(data)
  138. });
  139. const result = await response.json();
  140. console.log(result);
  141. // 响应处理
  142. if (result) {
  143. alert('添加成功');
  144. // 跳到首页
  145. location.href = 'index.php';
  146. } else {
  147. alert('添加失败');
  148. location.href = 'register.php';
  149. btn.form.email.focus();
  150. }
  151. }
  152. </script>
【文章原创作者:建湖网站设计公司 http://www.1234xp.com/jianhu.html 提供,感恩】