Blogger Information
Blog 43
fans 4
comment 0
visits 19261
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP会话控制/简单的登录注册功能
汇享科技
Original
547 people have browsed it
  • 效果演示
    没时间美化登录注册界面下次在美化 其他部分代码比较多 就把控制器的代码拿出来了
  • 代码部分
  1. heand.php
  1. <?php
  2. //链接数据库文件
  3. require 'config/catenate.php';
  4. //开启会话
  5. session_start();
  6. //查询用户数据
  7. $stmt = $db->prepare('SELECT * FROM `user`;');
  8. if ($stmt->execute()) {
  9. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  10. } else {
  11. print_r($stmt->errorInfo());
  12. }
  13. //判断请求类型
  14. $action = strtolower($_GET['action']);
  15. // print_r($action);
  16. switch ($action) {
  17. //登录
  18. case 'login':
  19. //判断是否为POST请求
  20. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  21. //获取用户登录传进的数据
  22. $email = $_POST['email'];
  23. $password = sha1($_POST['password']);
  24. //从数据库中进行对比
  25. $res = array_filter($users, function ($user) use ($email, $password) {
  26. return $user['email'] === $email && $user['password'] === $password;
  27. });
  28. if (count($res) === 1) {
  29. //写入session
  30. $_SESSION['user'] = serialize(array_pop($res));
  31. exit('<script>alert("登录成功");location.href="index.php";</script>');
  32. } else {
  33. exit('<script>alert("账户或密码错误,或者还没有帐号");location.href="login.php";</script>');
  34. }
  35. } else {
  36. die('请求类型错误');
  37. }
  38. //注册
  39. case 'reg':
  40. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  41. //获取用户注册传进的数据
  42. $name = $_POST['name'];
  43. $email = $_POST['email'];
  44. $password = sha1($_POST['p1']);
  45. $reg_time = time();
  46. // 将数据写入数据库
  47. // $sql = "INSERT `info` SET `biaoti`=:biaoti, `neirong` = :neirong, `time`= :sjtime ";
  48. $sql = <<<SQL
  49. INSERT `user`
  50. SET `name`= ?,
  51. `email` = ?,
  52. `password` = ?,
  53. `reg_time` = ?
  54. SQL;
  55. $stmt = $db->prepare($sql);
  56. $data = [$name, $email, $password, $reg_time];
  57. $stmt->execute($data);
  58. if ($stmt->rowCount() === 1) {
  59. $sql = 'SELECT * FROM `user` WHERE `id` = ' . $db->lastInsertId();
  60. $stmt = $db->prepare($sql);
  61. $stmt->execute();
  62. $newUser = $stmt->fetch(PDO::FETCH_ASSOC);
  63. $_SESSION['user'] = serialize($newUser);
  64. exit('<script>alert("注册成功");location.assign("index.php")</script>');
  65. } else {
  66. exit('<script>alert("注册失败");location.assign("login.php")</script>');
  67. }
  68. }
  69. //退出
  70. case 'logout':
  71. if (isset($_SESSION['user'])) {
  72. session_destroy();
  73. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  74. }
  75. default:
  76. exit('未定义的操作');
  77. }
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!