Blogger Information
Blog 34
fans 0
comment 0
visits 20525
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php灭绝师太手把手教你注册信息验证
小庄
Original
612 people have browsed it

php灭绝师太手把手教你注册信息验证

  1. <?php
  2. //根据需求生成验证码
  3. function getRandomString($len, $chars=null)
  4. {
  5. if (is_null($chars)) {
  6. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  7. }
  8. mt_srand(10000000*(double)microtime());
  9. for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
  10. $str .= $chars[mt_rand(0, $lc)];
  11. }
  12. return $str;
  13. }
  14. //生成验证码
  15. function getRandomString1(){
  16. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  17. for($i = 0;$i < 4;$i++){
  18. $code .= $chars{mt_rand(0,strlen($chars)-1)};
  19. }
  20. return $code;
  21. }
  22. //调用验证码函数生成验证码,赋值到变量以便后面使用
  23. $yanzhengma = getRandomString(6);
  24. ?>
  25. <!DOCTYPE html>
  26. <html lang="en">
  27. <head>
  28. <meta charset="UTF-8">
  29. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  30. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  31. <title>Document</title>
  32. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
  33. <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  34. <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
  35. </head>
  36. <body>
  37. <div class="container">
  38. <form action="index2.php" method="post">
  39. <div class="form-group">
  40. <label for="exampleInputEmail1">邮箱</label>
  41. <input type="email" class="form-control" name="exampleInputEmail1" id="exampleInputEmail1" aria-describedby="emailHelp">
  42. <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  43. </div>
  44. <div class="form-group">
  45. <label for="exampleInputPassword1">密码</label>
  46. <input type="password" name="exampleInputPassword1" class="form-control" id="exampleInputPassword1">
  47. </div>
  48. <div class="form-group">
  49. <label for="exampleInputPassword2">密码</label>
  50. <input type="password" name="exampleInputPassword2" class="form-control" id="exampleInputPassword2">
  51. </div>
  52. <div class="form-group">
  53. <label for="exampleInputyanzhengma">验证码:</label>
  54. <?=$yanzhengma?><?=$errorsyzm?>
  55. <input type="text" name="exampleInputyanzhengma" class="form-control" id="exampleInputyanzhengma">
  56. <input type="hidden" name="hiddenyanzhengma" value="<?=$yanzhengma?>">
  57. </div>
  58. <button type="submit" name="tijiao" value="1" class="btn btn-primary">登录</button>
  59. </form>
  60. </div>
  61. </body>
  62. </html>
  63. <?php
  64. //获取用户名密码
  65. $email = $_POST["exampleInputEmail1"];
  66. $pwd1 = $_POST["exampleInputPassword1"];
  67. $pwd2 = $_POST["exampleInputPassword2"];
  68. //获取验证码与隐藏验证码,隐藏验证码用于页面提交后(等于刷新了也i按)生成新的验证码,而无法匹配
  69. $yanzheng = $_POST["exampleInputyanzhengma"];
  70. $hiddenyanzhengma = $_POST["hiddenyanzhengma"];
  71. $tijiao = $_POST["tijiao"];
  72. if($tijiao == 1){
  73. if(strlen($email) > 10) {
  74. if($pwd1 == $pwd2) {
  75. if(strlen($pwd2) > 8) {
  76. if($yanzheng === $hiddenyanzhengma) {
  77. echo '注册成功';
  78. }else{
  79. echo '<span style="color:red">验证码输入错误</span>';
  80. }
  81. }else{
  82. echo '<span style="color:red">密码长度至少为8位</span>';
  83. }
  84. }else{
  85. echo '<span style="color:red">两次输入密码不一致</span>';
  86. }
  87. }else{
  88. echo '<span style="color:red">邮箱长度至少10位</span>';
  89. }
  90. }
  91. ?>
Correcting teacher:灭绝师太灭绝师太

Correction status:qualified

Teacher's comments:作业写的很好, 验证码的英文是captcha,最好用对应的英文命名变量哈~
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!