Blogger Information
Blog 26
fans 0
comment 0
visits 11929
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
用户登录表单数据的前端处理与后端PHP处理流程附流程图和关键JS和PHP源码(1125作业)
高空中的云
Original
696 people have browsed it

流程图

关键源码

  1. // email和password是从表单中获取到的邮箱和密码值
  2. // 有效性校验,这样做的好处是减少一些无效请求,减轻服务器压力和相关开销
  3. const emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
  4. // 假定密码规则是需要介于6~12位,且包含大小写和数字及特殊字符(!@#%&)
  5. const passwordReg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#%&]).{6,12}$/;
  6. if(
  7. !emailReg.test(email) || !passwordReg.test(password)
  8. ){
  9. alert('邮箱或密码格式不正确');
  10. return false;
  11. }else{
  12. // 向后端提交的代码写到这里
  13. }
  1. // 前后端双重校验,减少出错概率
  2. //$user 为前端传过来的email和password数组
  3. // $emailReg = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";
  4. $passwordReg = "/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#%&]).{6,12}$/";
  5. if(
  6. !filter_var($user['email'],FILTER_VALIDATE_EMAIL)
  7. ||
  8. !preg_match($passwordReg,$user['password'])
  9. ){
  10. echo json_encode([
  11. 'state'=> -1, // -1, 账号密码失效
  12. 'msg' => '账号或密码错误'
  13. ]);
  14. }else{
  15. // 假定函数checkUserfromDatabase($email,$password) 是向数据库中查找一条数据,email和password是否匹配;如存在,返回true,否则false
  16. if(checkUserfromDatabase($user['email'],$user['password'])){
  17. echo json_encode([
  18. 'state' => 1,
  19. 'msg' => '登录成功'
  20. ]);
  21. }else{
  22. echo json_encode([
  23. 'state'=> -1, // -1, 账号密码失效
  24. 'msg' => '账号或密码错误'
  25. ]);
  26. }
  27. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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!