php之字符串实战注册页面

1-5 C ABD C C A
6-10 AD D B C A
11-15 A B ACD D A
16-23 B B D C B C B C

代码块

前端

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>注册</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. box-sizing: border-box;
  11. }
  12. body{
  13. background: dodgerblue;
  14. }
  15. #form{
  16. border-radius: 12px;
  17. width: 20em;
  18. height: 20em;
  19. margin: 1em auto;
  20. position: relative;
  21. top: 10em;
  22. background: #888888;
  23. }
  24. form {
  25. padding-left: 2em;
  26. padding-top: 7em;
  27. }
  28. #title{
  29. position: absolute;
  30. padding-left: 6em;
  31. padding-top: 2em;
  32. font-size: 20px;
  33. }
  34. #submit input{
  35. width: 5em;
  36. height: 2em;
  37. }
  38. #submit {
  39. padding-top: 1em;
  40. padding-left: 6em;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div id="form">
  46. <div id="title">用户注册</div>
  47. <form action="demo.php" style="display: grid;gap: 0.5em" method="post">
  48. <div>
  49. <label>昵&emsp;&emsp;称*<input type="text" name="username" autofocus placeholder="用户名为2-16位"></label>
  50. </div>
  51. <div>
  52. <label>邮&emsp;&emsp;箱*<input type="email" name="email" placeholder="demo@email.com"></label>
  53. </div>
  54. <div>
  55. <label>密&emsp;&emsp;码*<input type="password" name="pwd" placeholder="密码为6-16位"></label>
  56. </div>
  57. <div>
  58. <label>确认密码*<input type="password" name="pwd_two" placeholder="密码为6-16位"></label>
  59. </div>
  60. <div>
  61. <!-- secret绑定input的id-->
  62. <label for="secret">性别:</label>
  63. <label><input type="radio" name="sex" id="man" value="man"></label>
  64. <label><input type="radio" name="sex" id="woman" value="woman"></label>
  65. <label><input type="radio" name="sex" id="secret" value="secret" checked>保密</label>
  66. </div>
  67. <div id="submit">
  68. <input type="submit" value="注册" >
  69. </div>
  70. </form>
  71. </div>
  72. </body>
  73. </html>

后端

  1. <?php
  2. $data = $_POST;
  3. if (empty($data)) {
  4. header("refresh:1;url=http://www.test.com");
  5. exit("数据不能为空....");
  6. }
  7. $arr = ['username' => '用户名','email'=>'邮箱','pwd' => '密码'];
  8. foreach ($arr as $k => $v) {
  9. if (empty($data[$k])) {
  10. header("refresh:1;url=http://www.test.com");
  11. exit($v."不能为空");
  12. }
  13. }
  14. if ($data['pwd'] != $data['pwd_two']) {
  15. header("refresh:1;url=http://www.test.com");
  16. exit("密码不一致");
  17. }
  18. if(!preg_match('/^[A-Za-z0-9_\x{4e00}-\x{9fa5}]+$/u',$data['username'])) {
  19. header("refresh:1;url=http://www.test.com");
  20. exit("用户名由2-16位数字或字母、汉字、下划线组成!");
  21. }
  22. if(!preg_match('/(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,16}/', $data['pwd'])) {
  23. header("refresh:1;url=http://www.test.com");
  24. exit("密码由6-16位字符(英文/数字/符号)三种组合!");
  25. }
  26. header("refresh:1;url=http://www.test.com");
  27. exit("注册成功");

页面展示