Blogger Information
Blog 50
fans 0
comment 0
visits 31583
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
脚本的灵活运用
手机用户1580651468
Original
679 people have browsed it

脚本的灵活运用

一:将用户登录,注册,退出等常用功能使用一个脚本统一处理 作业

一)实现的代码文档块

  1. <?php
  2. // 开启会话
  3. session_start();
  4. $allowOpts=['login','register','logout'];
  5. // 1.导入用户数据
  6. require __DIR__.'/../../config/common.php';
  7. $users=require DATA_PATH.'/users.php';
  8. // 2.获取get数据
  9. $action=$_GET['action'];
  10. // 3,允许值过滤
  11. $allow=array_filter($allowOpts,function($allow)use($action){ return $allow===$action;});
  12. switch (count($allow)) {
  13. //---------------1.登录模块--------//
  14. case $action==='login':
  15. $email=$_GET['email'];
  16. $password = md5($_GET['password']);
  17. $result = array_filter($users,function($user) use ($email,$password){ return $user['email']=== $email&&$user['password'] === $password; });
  18. $prompt=false;
  19. if(count($result)===1){$prompt=true;$_SESSION['user'] =array_pop($result);}
  20. echo json_encode($prompt);
  21. break;
  22. //---------------1.登录模块--------//
  23. //---------------2.用户注册模块--------//
  24. case $action==='register':
  25. // 1).数组长度
  26. $oriCount = count($users);
  27. //2).接收数据
  28. $json = file_get_contents('php://input');
  29. //3).json转数组
  30. $user = json_decode($json,true);
  31. // 4).创建新用户数据
  32. $user['password'] = md5($user['password']);
  33. $user['id'] = count($users)+1;
  34. //5).添加新用户
  35. $users[]=$user;
  36. // 6).分析结果
  37. $prompt=false;
  38. if(count($users)===$oriCount+1){$prompt=true;}
  39. echo json_encode($prompt);
  40. break;
  41. //---------------2.用户注册模块--------//
  42. //---------------3.用户退出模块--------//
  43. case $action==='logout':
  44. $prompt=false;
  45. if(session_destroy()){ $prompt=true;}
  46. echo json_encode($prompt);
  47. break;
  48. //---------------3.用户退出模块--------//
  49. default:
  50. echo '请求错误';
  51. break;
  52. }

二)实现的效果图



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