小实战:注册登录功能

编辑: admin 分类: 电脑知识 发布时间: 2023-06-14 来源:互联网
一、index.php
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <?php
  5. $arr = require 'data/data.php';
  6. ?>
  7. <meta charset="UTF-8" />
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  10. <meta name="keywords" content="<?= $arr[0] ?>" />
  11. <meta name="description" content="<?= $arr[1] ?>" />
  12. <link rel="stylesheet" href="static/css/style.css" />
  13. <title><?= $arr[2] ?></title>
  14. </head>
  15. <body>
  16. <?php
  17. // 引入配置文件
  18. include __DIR__. '/config/common.php';
  19. // 引入头部 -->
  20. include TMPL_PATH_PUBLIC. '/header.php';
  21. // 引入新闻数据 -->
  22. $news = include DATA_PATH . '/news.php';
  23. // 引入产品数据 -->
  24. $items = include DATA_PATH . '/items.php';
  25. $users = include DATA_PATH . '/users.php';
  26. ?>
  27. <!-- 主体 -->
  28. <main>
  29. <!-- 新闻列表 -->
  30. <div class="news">
  31. <h3>新闻列表</h3>
  32. <div class="list">
  33. <? foreach ($news as $k => $new) :
  34. extract($new);
  35. if ($k<5) :
  36. ?>
  37. <a href="<?= $url ?>" target=_blank>
  38. <?= mb_substr($title,0,35) ?>...
  39. </a>
  40. <? endif ?>
  41. <? endforeach ?>
  42. </div>
  43. </div>
  44. <!-- 产品列表 -->
  45. <div class="items">
  46. <h3>产品列表</h3>
  47. <div class="list">
  48. <? foreach ($items as $k => $item) :
  49. extract($item);
  50. if ($k<4) :
  51. ?>
  52. <div class="item">
  53. <img src="<?= $img ?>" alt="<?= $title ?>" />
  54. <a href="<?= $url ?>" target=_blank>
  55. <?= mb_substr($title,0,9) ?>...
  56. </a>
  57. </div>
  58. <? endif ?>
  59. <? endforeach ?>
  60. </div>
  61. </div>
  62. </main>
  63. <!-- 引入页脚 -->
  64. <?php include 'template/public/footer.php' ?>
  65. </body>
  66. </html>

二、register.php
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <meta name="keywords" content="新闻,产品,合肥" />
  8. <meta name="description" content="专业生产中高档办公家具,行业畅销20年" />
  9. <link rel="stylesheet" href="static/css/style.css" />
  10. <title>注册</title>
  11. </head>
  12. <body>
  13. <?php
  14. // 引入配置文件
  15. include __DIR__. '/config/common.php';
  16. // 引入头部 -->
  17. include TMPL_PATH_PUBLIC. '/header.php';
  18. ?>
  19. <!-- 主体 -->
  20. <main>
  21. <form class="login" id="register" method="post" action="">
  22. <table>
  23. <caption>用户注册</caption>
  24. <tbody>
  25. <tr>
  26. <td><label for="uname">昵称:</label></td>
  27. <td><input type="text" name="uname" id="uname" placeholder="uname" required autofocus /></td>
  28. </tr>
  29. <tr>
  30. <td><label for="email">邮箱:</label></td>
  31. <td><input type="email" name="email" id="email" placeholder="username@email.com" required /></td>
  32. </tr>
  33. <tr>
  34. <td><label for="password">密码:</label></td>
  35. <td><input type="password" name="password" id="password" placeholder="******" required /></td>
  36. </tr>
  37. <tr>
  38. <td><label for="rePassword">确认:</label></td>
  39. <td><input type="password" name="rePassword" id="rePassword" placeholder="******" required /></td>
  40. </tr>
  41. <tr>
  42. <td colspan="2"><button type="button" onclick="addUser(this)">提交</button></td>
  43. </tr>
  44. </tbody>
  45. </table>
  46. </form>
  47. <p>
  48. <a href="login.html">已有帐号,请直接登录</a>
  49. </p>
  50. </main>
  51. <!-- 引入页脚 -->
  52. <?php include 'template/public/footer.php' ?>
  53. </body>
  54. </html>
  55. <script>
  56. function addUser(btn) {
  57. // 1. 获取用户的输入
  58. const user = getInput(btn.form);
  59. // 2. 非空验证
  60. if (isEmpty(user)) {
  61. // 3. 验证二次密码是否相等?
  62. if (isPswEqu(user)) {
  63. // 4. 创建提交的数据JSON
  64. const data = createData(user);
  65. // 5. 异步提交
  66. insertUser(data);
  67. }
  68. }
  69. }
  70. // 1. 获取用户输入
  71. const getInput = (form) => {
  72. return {
  73. // 呢称:
  74. uname: {
  75. // dom元素
  76. ele: form.uname,
  77. // 值
  78. value: form.uname.value.trim()
  79. },
  80. // 邮箱:
  81. email: {
  82. ele: form.email,
  83. value: form.email.value.trim()
  84. },
  85. // 密码:
  86. password: {
  87. ele: form.password,
  88. value: form.password.value.trim()
  89. },
  90. // 确认密码:
  91. rePassword: {
  92. ele: form.rePassword,
  93. value: form.rePassword.value.trim()
  94. },
  95. }
  96. }
  97. // 2. 非空验证
  98. const isEmpty = (user) => {
  99. switch (true) {
  100. case user.uname.value.length === 0:
  101. alert('呢称不能为空');
  102. user.uname.ele.focus();
  103. return false;
  104. case user.email.value.length === 0:
  105. alert('邮箱不能为空');
  106. user.email.ele.focus();
  107. return false;
  108. case user.password.value.length === 0:
  109. alert('密码不能为空');
  110. user.password.ele.focus();
  111. return false;
  112. case user.rePassword.value.length === 0:
  113. alert('重复密码不能为空');
  114. user.rePassword.ele.focus();
  115. return false;
  116. default:
  117. return true;
  118. }
  119. }
  120. // 3. 验证二次密码是否相等?
  121. const isPswEqu = (user) => {
  122. if (user.password.value !== user.rePassword.value) {
  123. alert('二次密码不相等,重新输入');
  124. user.password.ele.focus();
  125. return false;
  126. } else {
  127. return true;
  128. }
  129. }
  130. // 4. 创建提交的数据JSON
  131. const createData = (user) => {
  132. return {
  133. uname: user.uname.value,
  134. email: user.email.value,
  135. password: user.password.value,
  136. }
  137. }
  138. // 5. 异步提交
  139. async function insertUser(data) {
  140. console.log(data);
  141. const url = './lib/userHandle.php?action=register';
  142. const response = await fetch(url, {
  143. method: 'POST',
  144. headers: {
  145. 'content-type': 'application/json;charset=utf-8'
  146. },
  147. body: JSON.stringify(data)
  148. });
  149. const result = await response.json();
  150. console.log(result);
  151. // 响应处理
  152. if (result) {
  153. alert('添加成功');
  154. // 跳到首页
  155. location.href = 'index.php';
  156. } else {
  157. alert('添加失败');
  158. location.href = 'register.php';
  159. btn.form.email.focus();
  160. }
  161. }
  162. </script>

三、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. switch ($action):
  9. case 'login':
  10. // 1. 获取用户登录数据,不能用传统的表单格式提交
  11. // json当成文本流原始数据接收
  12. $json = file_get_contents('php://input');
  13. // 2. $json 并非php能识别的数据类型,它只是json格式的字符串而已
  14. // json -> php.array, true: 数组
  15. $user = json_decode($json, true);
  16. // 3. 解析用户提交的数据,做预处理判断:
  17. $email = $user['email'];
  18. $salt = 'wwwphpcn';
  19. $password = md5($user['password'] . $salt);
  20. // 4. 用户过滤
  21. $res = array_filter($users,function($item) use ($email, $password) {
  22. return $item['email'] === $email && $item['password'] === $password;
  23. });
  24. $res = array_values($res);
  25. // 5. 结果分析
  26. // 默认检验用户信息 登录不通过
  27. if (count($res) === 1) {
  28. $prompt = true;
  29. $_SESSION['user']['email'] = $res[0]['email'];
  30. $_SESSION['user']['name'] = $res[0]['name'];
  31. }
  32. break;
  33. case 'logout':
  34. if(session_destroy()){
  35. $prompt = true;
  36. };
  37. break;
  38. case 'register':
  39. $oriCount = count($users);
  40. $json = file_get_contents('php://input');
  41. $user = json_decode($json, true);
  42. $user['password'] = md5($user['password']);
  43. $user['id'] = count($users) + 1;
  44. $users[] = $user;
  45. if (count($users) === $oriCount + 1) {
  46. $prompt = true;
  47. }
  48. break;
  49. endswitch;
  50. echo json_encode($prompt);

四、相关截图



五、疑问:能否获取了users.php文件数据,然后注册的时候重写进去呢,然后就可以直接用新注册的用户数组进行登录呢。" class="reference-link">能否获取了users.php文件数据,然后注册的时候重写进去呢,然后就可以直接用新注册的用户数组进行登录呢。【文章出处:香港站群多ip服务器 http://www.558idc.com/hkzq.html提供,感恩】