Blogger Information
Blog 38
fans 0
comment 0
visits 22716
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
用户注销-表单验证及heredoc, nowdoc用法
一个好人
Original
508 people have browsed it

用户注销:

首先在主页表头添加点击事件,logout方法

  1. async function logout(){
  2. if (confirm('是否退出')) {
  3. // const url = './lib/userHandle.php?action=logout';
  4. const url = './lib/user/logout.php';
  5. const response = await fetch(url);
  6. const result = await response.json();
  7. if(result){
  8. alert('退出成功');
  9. location.href='index.php';
  10. }else{
  11. alert('退出失败');
  12. location.href='login.php';
  13. }
  14. }
  15. }

在logout文件中销毁session

  1. session_start();
  2. $flag = false;
  3. if(session_destroy()){
  4. $flag = true;
  5. }
  6. echo json_decode($flag);

表单验证

表单验证主要有获取数据,非空验证,两次密码相同验证,插入数据等几步,可分别用函数验证;数据插入部分尚未完成;

  1. function addUser(btn){
  2. const user = getInput(btn.form)
  3. alert(JSON.stringify(user));
  4. if(isEmpty(user)){
  5. if(isPswEqu(user)){
  6. const data = createData(user)
  7. insertUser(data);
  8. }
  9. }
  10. }
  11. const getInput = (form) =>{
  12. return {
  13. uname:{
  14. ele:form.uname,
  15. value:form.uname.value.trim()
  16. }, email:{
  17. ele:form.email,
  18. value:form.email.value.trim()
  19. },psw1:{
  20. ele:form.psw1,
  21. value:form.psw1.value.trim()
  22. },psw2:{
  23. ele:form.psw2,
  24. value:form.psw2.value.trim()
  25. }
  26. }
  27. }
  28. const isEmpty=(user)=>{
  29. switch(true){
  30. case user.uname.value.length ==0:
  31. alert('用户名不能为空');
  32. user.uname.ele.focus();
  33. return false;
  34. case user.email.value.length ==0:
  35. alert('邮箱不能为空');
  36. user.email.ele.focus();
  37. return false;
  38. case user.psw1.value.length ==0:
  39. alert('用户名不能为空');
  40. user.psw1.ele.focus();
  41. return false;
  42. case user.psw2.value.length ==0:
  43. alert('用户名不能为空');
  44. user.psw2.ele.focus();
  45. return false;
  46. default:
  47. return true;
  48. }
  49. const isPswEqu = (user)=>{
  50. if(user.psw1.value !== user.psw2.value){
  51. alert('两次密码输入不一致');
  52. user.psw1.ele.focus();
  53. return false;
  54. }else{
  55. return true;
  56. }
  57. }
  58. }
  59. const createData = ()=>{
  60. return{
  61. uname:user.uname.value,
  62. email:user.email.value,
  63. password:user.psw1.value,
  64. }
  65. }
  66. async function insertUser(data){
  67. const response = await fetch('./lib/userHandle.php?action=register', {
  68. method:'POST',
  69. headers:{
  70. 'content-type':'application/json;charset=utf-8'
  71. },
  72. body:JSON.stringify(data)
  73. });
  74. const result =response.json();
  75. if(result){
  76. alert('注册成功');
  77. location.href = 'index.php'
  78. }else{
  79. alert('注册失败');
  80. location.href='register.php';
  81. btn.form.uname.focus();
  82. }
  83. }

heredoc/nowdoc:

多行字符串,heredoc格式<<<ABC aaa ABC;可转译换行符\n等;nowdoc格式<<<ABC ‘aaa’ ABC;不转译;

  1. if(!in_array($action, $allowOpts)){
  2. echo <<<ABC
  3. <script>
  4. alert('非法访问‘);
  5. location.href='/../login.php';
  6. </script>
  7. ABC;
  8. }
  9. // ''不能转译,""可以转译换行符\n等
  10. // 定界符heredoc,解析变量
  11. $str = <<<POEM
  12. // 定界符nowdoc,不解析变量
  13. <<< 'POEM'
  14. POEM;

总结:

建议大家userHandle还没写好,最好不要把路径变过去,如果调试到半路停下了,登录、注销也用不了了!

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