博客列表 >PHP实例演示会话控制/简单的登录注册功能

PHP实例演示会话控制/简单的登录注册功能

早晨
早晨原创
2022年08月25日 17:03:23666浏览

首页登录入口

  1. <?php
  2. namespace _0825;
  3. session_start();
  4. // 判断是否已登录?
  5. if (isset($_SESSION['user'])) {
  6. $us = unserialize($_SESSION['user']);
  7. }
  8. ?>
  9. <!DOCTYPE html>
  10. <html lang="zh-CN">
  11. <head>
  12. <meta charset="UTF-8">
  13. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>首页登录入口</title>
  16. </head>
  17. <style>
  18. nav {
  19. height: 40px;
  20. background-color: deepskyblue;
  21. padding: 0 20px;
  22. display: flex;
  23. justify-content: space-between;
  24. align-items: center;
  25. }
  26. nav>a {
  27. color: white;
  28. text-decoration: none;
  29. }
  30. </style>
  31. <body>
  32. <nav>
  33. <a href="index.php">我的博客</a>
  34. <?php if (isset($us)) : ?>
  35. <span style="margin-left: 400px">用户名:<?= $us['name'] ?></span>
  36. <span style="color: red;">邮箱:<?= $us['email'] ?></span>
  37. <a id="logout">退出</a>
  38. <?php else : ?>
  39. <a href="login.php">登录</a>
  40. <?php endif ?>
  41. </nav>
  42. <script>
  43. document.querySelector('#logout').addEventListener('click', function(event) {
  44. if (confirm('是否退出')) {
  45. event.preventDefault();
  46. window.location.assign("top.php?us=logout");
  47. }
  48. });
  49. </script>
  50. </body>
  51. </html>

用户登录login.php

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>用户登录</title>
  8. <link rel="stylesheet" href="css/font-awesome.min.css">
  9. <link rel="stylesheet" href="css/bootstrap.min.css">
  10. <style type="text/css">
  11. body,
  12. html {
  13. font-size: 100%;
  14. padding: 0;
  15. margin: 0;
  16. }
  17. /* Reset */
  18. *,
  19. *:after,
  20. *:before {
  21. -webkit-box-sizing: border-box;
  22. -moz-box-sizing: border-box;
  23. box-sizing: border-box;
  24. }
  25. .clearfix:before,
  26. .clearfix:after {
  27. content: " ";
  28. display: table;
  29. }
  30. .clearfix:after {
  31. clear: both;
  32. }
  33. body {
  34. background: #00b4ef;
  35. color: #D5D6E2;
  36. font-weight: 500;
  37. font-size: 1.05em;
  38. font-family: "Microsoft YaHei", "Segoe UI", "Lucida Grande", Helvetica, Arial, sans-serif;
  39. }
  40. a {
  41. color: rgba(255, 255, 255, 0.6);
  42. outline: none;
  43. text-decoration: none;
  44. -webkit-transition: 0.2s;
  45. transition: 0.2s;
  46. }
  47. a:hover,
  48. a:focus {
  49. color: #74777b;
  50. text-decoration: none;
  51. }
  52. .form-horizontal {
  53. background: #fff;
  54. padding-bottom: 40px;
  55. border-radius: 15px;
  56. text-align: center;
  57. }
  58. .form-horizontal .heading {
  59. display: block;
  60. font-size: 35px;
  61. font-weight: 700;
  62. padding: 35px 0;
  63. border-bottom: 1px solid #f0f0f0;
  64. margin-bottom: 30px;
  65. }
  66. .form-horizontal .form-group {
  67. padding: 0 40px;
  68. margin: 0 0 25px 0;
  69. position: relative;
  70. }
  71. .form-horizontal .form-control {
  72. background: #f0f0f0;
  73. border: none;
  74. border-radius: 20px;
  75. box-shadow: none;
  76. padding: 0 20px 0 45px;
  77. height: 40px;
  78. transition: all 0.3s ease 0s;
  79. }
  80. .form-horizontal .form-control:focus {
  81. background: #e0e0e0;
  82. box-shadow: none;
  83. outline: 0 none;
  84. }
  85. .form-horizontal .form-group i {
  86. position: absolute;
  87. top: 12px;
  88. left: 60px;
  89. font-size: 17px;
  90. color: #c8c8c8;
  91. transition: all 0.5s ease 0s;
  92. }
  93. .form-horizontal .form-control:focus+i {
  94. color: #00b4ef;
  95. }
  96. .form-horizontal .fa-question-circle {
  97. display: inline-block;
  98. position: absolute;
  99. top: 12px;
  100. right: 60px;
  101. font-size: 20px;
  102. color: #808080;
  103. transition: all 0.5s ease 0s;
  104. }
  105. .form-horizontal .fa-question-circle:hover {
  106. color: #000;
  107. }
  108. .form-horizontal .main-checkbox {
  109. float: left;
  110. width: 20px;
  111. height: 20px;
  112. background: #11a3fc;
  113. border-radius: 50%;
  114. position: relative;
  115. margin: 5px 0 0 5px;
  116. border: 1px solid #11a3fc;
  117. }
  118. .form-horizontal .main-checkbox label {
  119. width: 20px;
  120. height: 20px;
  121. position: absolute;
  122. top: 0;
  123. left: 0;
  124. cursor: pointer;
  125. }
  126. .form-horizontal .main-checkbox label:after {
  127. content: "";
  128. width: 10px;
  129. height: 5px;
  130. position: absolute;
  131. top: 5px;
  132. left: 4px;
  133. border: 3px solid #fff;
  134. border-top: none;
  135. border-right: none;
  136. background: transparent;
  137. opacity: 0;
  138. -webkit-transform: rotate(-45deg);
  139. transform: rotate(-45deg);
  140. }
  141. .form-horizontal .main-checkbox input[type=checkbox] {
  142. visibility: hidden;
  143. }
  144. .form-horizontal .main-checkbox input[type=checkbox]:checked+label:after {
  145. opacity: 1;
  146. }
  147. .form-horizontal .text {
  148. float: left;
  149. margin-left: 7px;
  150. line-height: 20px;
  151. padding-top: 5px;
  152. text-transform: capitalize;
  153. }
  154. .form-horizontal .text a {
  155. color: #000;
  156. }
  157. .form-horizontal .text a:hover {
  158. color: red;
  159. }
  160. .form-horizontal .btn {
  161. float: right;
  162. font-size: 14px;
  163. color: #fff;
  164. background: #00b4ef;
  165. border-radius: 30px;
  166. padding: 10px 25px;
  167. border: none;
  168. text-transform: capitalize;
  169. transition: all 0.5s ease 0s;
  170. }
  171. @media only screen and (max-width: 479px) {
  172. .form-horizontal .form-group {
  173. padding: 0 25px;
  174. }
  175. .form-horizontal .form-group i {
  176. left: 45px;
  177. }
  178. .form-horizontal .btn {
  179. padding: 10px 20px;
  180. }
  181. }
  182. </style>
  183. </head>
  184. <body>
  185. <div class="demo" style="padding: 20px 0;">
  186. <div class="container">
  187. <div class="row">
  188. <div class="col-md-offset-3 col-md-6">
  189. <form class="form-horizontal" action="top.php?us=login" method="post" onsubmit="return compare()">
  190. <span class="heading">用户登录</span>
  191. <div class="form-group">
  192. <input type="email" class="form-control" name="email" id="inputEmail" placeholder="请输入邮箱">
  193. <i class="fa fa-user"></i>
  194. </div>
  195. <div class="form-group help">
  196. <input type="password" class="form-control" name="password" id="Password" placeholder="请输入密码">
  197. <i class="fa fa-lock"></i>
  198. </div>
  199. <div class="form-group">
  200. <span class="text"><a href="reg.php">注册用户</a></span>
  201. <button type="submit" class="btn btn-default">登录</button>
  202. </div>
  203. </form>
  204. </div>
  205. </div>
  206. </div>
  207. </div>
  208. </body>
  209. </html>

注册用户reg.php

  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>注册用户</title>
  8. <link rel="stylesheet" href="css/font-awesome.min.css">
  9. <link rel="stylesheet" href="css/bootstrap.min.css">
  10. <style type="text/css">
  11. body,
  12. html {
  13. font-size: 100%;
  14. padding: 0;
  15. margin: 0;
  16. }
  17. /* Reset */
  18. *,
  19. *:after,
  20. *:before {
  21. -webkit-box-sizing: border-box;
  22. -moz-box-sizing: border-box;
  23. box-sizing: border-box;
  24. }
  25. .clearfix:before,
  26. .clearfix:after {
  27. content: " ";
  28. display: table;
  29. }
  30. .clearfix:after {
  31. clear: both;
  32. }
  33. body {
  34. background: #00b4ef;
  35. color: #D5D6E2;
  36. font-weight: 500;
  37. font-size: 1.05em;
  38. font-family: "Microsoft YaHei", "Segoe UI", "Lucida Grande", Helvetica, Arial, sans-serif;
  39. }
  40. a {
  41. color: rgba(255, 255, 255, 0.6);
  42. outline: none;
  43. text-decoration: none;
  44. -webkit-transition: 0.2s;
  45. transition: 0.2s;
  46. }
  47. a:hover,
  48. a:focus {
  49. color: #74777b;
  50. text-decoration: none;
  51. }
  52. .form-horizontal {
  53. background: #fff;
  54. padding-bottom: 40px;
  55. border-radius: 15px;
  56. text-align: center;
  57. }
  58. .form-horizontal .heading {
  59. display: block;
  60. font-size: 35px;
  61. font-weight: 700;
  62. padding: 35px 0;
  63. border-bottom: 1px solid #f0f0f0;
  64. margin-bottom: 30px;
  65. }
  66. .form-horizontal .form-group {
  67. padding: 0 40px;
  68. margin: 0 0 25px 0;
  69. position: relative;
  70. }
  71. .form-horizontal .form-control {
  72. background: #f0f0f0;
  73. border: none;
  74. border-radius: 20px;
  75. box-shadow: none;
  76. padding: 0 20px 0 45px;
  77. height: 40px;
  78. transition: all 0.3s ease 0s;
  79. }
  80. .form-horizontal .form-control:focus {
  81. background: #e0e0e0;
  82. box-shadow: none;
  83. outline: 0 none;
  84. }
  85. .form-horizontal .form-group i {
  86. position: absolute;
  87. top: 12px;
  88. left: 60px;
  89. font-size: 17px;
  90. color: #c8c8c8;
  91. transition: all 0.5s ease 0s;
  92. }
  93. .form-horizontal .form-control:focus+i {
  94. color: #00b4ef;
  95. }
  96. .form-horizontal .fa-question-circle {
  97. display: inline-block;
  98. position: absolute;
  99. top: 12px;
  100. right: 60px;
  101. font-size: 20px;
  102. color: #808080;
  103. transition: all 0.5s ease 0s;
  104. }
  105. .form-horizontal .fa-question-circle:hover {
  106. color: #000;
  107. }
  108. .form-horizontal .main-checkbox {
  109. float: left;
  110. width: 20px;
  111. height: 20px;
  112. background: #11a3fc;
  113. border-radius: 50%;
  114. position: relative;
  115. margin: 5px 0 0 5px;
  116. border: 1px solid #11a3fc;
  117. }
  118. .form-horizontal .main-checkbox label {
  119. width: 20px;
  120. height: 20px;
  121. position: absolute;
  122. top: 0;
  123. left: 0;
  124. cursor: pointer;
  125. }
  126. .form-horizontal .main-checkbox label:after {
  127. content: "";
  128. width: 10px;
  129. height: 5px;
  130. position: absolute;
  131. top: 5px;
  132. left: 4px;
  133. border: 3px solid #fff;
  134. border-top: none;
  135. border-right: none;
  136. background: transparent;
  137. opacity: 0;
  138. -webkit-transform: rotate(-45deg);
  139. transform: rotate(-45deg);
  140. }
  141. .form-horizontal .main-checkbox input[type=checkbox] {
  142. visibility: hidden;
  143. }
  144. .form-horizontal .main-checkbox input[type=checkbox]:checked+label:after {
  145. opacity: 1;
  146. }
  147. .form-horizontal .text {
  148. float: left;
  149. margin-left: 7px;
  150. line-height: 20px;
  151. padding-top: 5px;
  152. text-transform: capitalize;
  153. }
  154. .form-horizontal .text a {
  155. color: #000;
  156. }
  157. .form-horizontal .text a:hover {
  158. color: red;
  159. }
  160. .form-horizontal .btn {
  161. float: right;
  162. font-size: 14px;
  163. color: #fff;
  164. background: #00b4ef;
  165. border-radius: 30px;
  166. padding: 10px 25px;
  167. border: none;
  168. text-transform: capitalize;
  169. transition: all 0.5s ease 0s;
  170. }
  171. @media only screen and (max-width: 479px) {
  172. .form-horizontal .form-group {
  173. padding: 0 25px;
  174. }
  175. .form-horizontal .form-group i {
  176. left: 45px;
  177. }
  178. .form-horizontal .btn {
  179. padding: 10px 20px;
  180. }
  181. }
  182. </style>
  183. </head>
  184. <body>
  185. <div class="demo" style="padding: 20px 0;">
  186. <div class="container">
  187. <div class="row">
  188. <div class="col-md-offset-3 col-md-6">
  189. <form class="form-horizontal" action="top.php?us=reg" method="post" onsubmit="return compare()">
  190. <span class="heading">注册用户</span>
  191. <div class="form-group">
  192. <input type="text" class="form-control" name="name" id="inputName" placeholder="请输入昵称">
  193. <i class="fa fa-user"></i>
  194. </div>
  195. <div class="form-group">
  196. <input type="email" class="form-control" name="email" id="inputEmail" placeholder="请输入邮箱">
  197. <i class="fa fa-user"></i>
  198. </div>
  199. <div class="form-group help">
  200. <input type="password" class="form-control" name="password1" id="Pass1" placeholder="密码不少于8位">
  201. <i class="fa fa-lock"></i>
  202. </div>
  203. <div class="form-group help">
  204. <input type="password" class="form-control" name="password2" id="Pass2" placeholder="重复输入密码">
  205. <i class="fa fa-lock"></i>
  206. </div>
  207. <div class="form-group">
  208. <span class="text"><a href="login.php">用户登录</a></span>
  209. <button type="submit" class="btn btn-default" id="reg">提交注册</button>
  210. </div>
  211. </form>
  212. </div>
  213. </div>
  214. </div>
  215. </div>
  216. <script>
  217. function password() {
  218. if (document.forms[0].pass1.value.trim() !== document.forms[0].pass2.value.trim()) {
  219. document.querySelector('#reg').innerText = "两次密码不一样,请重新设置。";
  220. return false;
  221. }
  222. }
  223. </script>
  224. </body>
  225. </html>

会话处理top.php

  1. <?php
  2. namespace _0825;
  3. use PDO;
  4. // 开户会话
  5. session_start();
  6. // 连接数据库
  7. $db = new PDO('mysql:dbname=phpedu', 'root', '4889671');
  8. $stmt = $db->prepare('SELECT * FROM `admin_user`;');
  9. if ($stmt->execute()) {
  10. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  11. } else {
  12. print_r($stmt->errorInfo());
  13. }
  14. // 获取用户
  15. $us = strtolower($_GET['us']);
  16. switch ($us) {
  17. // 注册
  18. case 'reg':
  19. // 1. 获取新用户的数据
  20. $name = $_POST['name'];
  21. $email = $_POST['email'];
  22. $password = sha1(md5($_POST['password2']));
  23. $datetime = time();
  24. $sql = "INSERT `admin_user` SET `name`= ?, `email`= ?, `password`= ?, `datetime`= ?";
  25. $stmt = $db->prepare($sql);
  26. $data = [$name, $email, $password, $datetime];
  27. if ($stmt->execute($data)) {
  28. if ($stmt->rowCount() > 0) {
  29. // 注册成功之后,让用户自动登录
  30. $sql = 'SELECT * FROM `admin_user` WHERE `id` = ' . $db->lastInsertId();
  31. $stmt = $db->prepare($sql);
  32. $stmt->execute();
  33. $newUser = $stmt->fetch(PDO::FETCH_ASSOC);
  34. $_SESSION['user'] = serialize($newUser);
  35. exit('<script>alert("注册成功");location.href="index.php"</script>');
  36. } else {
  37. exit('<script>alert("注册失败");location.href="reg.php"</script>');
  38. }
  39. } else {
  40. print_r($stmt->errorInfo());
  41. }
  42. // 登录
  43. case 'login':
  44. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  45. $email = $_POST['email'];
  46. $password = sha1(md5($_POST['password']));
  47. $result = array_filter($users, function ($user) use ($email, $password) {
  48. return $user['email'] === $email && $user['password'] === $password;
  49. });
  50. if (count($result) === 1) {
  51. // 登录成功,写入session
  52. $_SESSION['user'] = serialize(array_pop($result));
  53. exit('<script>alert("验证通过");location.href="index.php"</script>');
  54. }
  55. exit('请求类型错误');
  56. }
  57. case 'logout':
  58. if (isset($_SESSION['user'])) {
  59. session_destroy();
  60. exit('<script>alert("退出成功");location.href="index.php"</script>');
  61. }
  62. default:
  63. exit('参数非法或未定义操作');
  64. }

运行效果

![![![](https://img.php.cn/upload/image/372/649/711/1661417934404408.jpg)](https://img.php.cn/upload/image/540/952/244/1661417923608889.jpg)](https://img.php.cn/upload/image/962/396/429/1661417915716768.jpg)

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议