博客列表 >用户注册与登录

用户注册与登录

P粉036614676
P粉036614676原创
2022年09月20日 17:22:44679浏览

当我们登录网站时,服务器就会给我们用户创建一个临时文件,用户存储SESSION数据,当服务器把页面传递给浏览器的时候,该SESSION数据会再浏览器中以cookie的形式存储

COOKIE

  1. <?php
  2. /**
  3. * 设置cookie,
  4. * 查看cookie
  5. */
  6. //1.设置一个;
  7. //setcookie('name','yk');
  8. //setrawcookie('site', 'php中文网');
  9. //echo $_COOKIE['name'];查看
  10. //2.设置多个
  11. setcookie('user[name]','yk');
  12. setcookie('user[password]','yksad');
  13. setcookie('user[email]','yksfd');
  14. /**
  15. * 设置多个cookie和多文件上传变量设置一样
  16. */
  17. if(isset($_COOKIE['user']))
  18. {
  19. foreach ($_COOKIE['user'] as $key => $value)
  20. {
  21. printf('%s-%s<br>',$key,$value);
  22. }
  23. }
  24. //3.删除cookie
  25. setcookie('user[name]',null,time()-3600);
  26. /**
  27. * 将过多信息存储在客户端,并不适合
  28. * 1. 数量受限: 30个
  29. * 2. 空间受限: 4k
  30. * 3. 安全隐患: 天生不可避免
  31. *
  32. * 所以, 会话信息推荐存储在服务器端
  33. * 客户端只需要保存一个会话ID,用于标识访问身份即可
  34. */

SESSION

  1. <?php
  2. /**
  3. * a.开启会话: session_start();
  4. * 启动会话做了二件事
  5. * 1. 浏览器: 创建会话ID: PHPSESSID, 一串md5加密的字符串
  6. * 2. 服务器: 创建与浏览器会话ID对应的会话文件,一个会话文件对应一个用户
  7. *
  8. * b.设置:
  9. *
  10. * c.更新:
  11. *
  12. * d.删除:
  13. * 1.unset()
  14. * 2.session_unset()
  15. * 3.session_destroy();
  16. */
  17. session_start();
  18. $_SESSION['name'] = 'yk';
  19. $_SESSION['password'] = md5('asdj') . 'asd';
  20. $_SESSION['name'] = 'update_yk';
  21. unset($_SESSION['name']);
  22. session_unset();
  23. session_destroy();

2.登录

CSS/index.php

  1. nav{
  2. height: 40px;
  3. background-color: deepskyblue;
  4. padding: 0 20px;
  5. display: flex;
  6. place-content: space-between;
  7. place-items: center;
  8. }
  9. nav>a{
  10. color: white;
  11. text-decoration: none;
  12. }

CSS/style.php

  1. body {
  2. display: flex;
  3. flex-direction: column;
  4. text-align: center;
  5. color: #555;
  6. font-weight: 300;
  7. /* background-color: lightcyan; */
  8. background: linear-gradient(to left, lightcyan, white);
  9. }
  10. body h3 {
  11. font-weight: 300;
  12. font-size: 20px;
  13. margin-bottom: 10px;
  14. }
  15. body form {
  16. width: 240px;
  17. padding: 20px;
  18. box-sizing: border-box;
  19. background: linear-gradient(to left top, lightskyblue, white);
  20. color: white;
  21. text-shadow: 0.5px 0.5px 0.5px #000;
  22. margin: auto;
  23. border-radius: 5px;
  24. box-shadow: 0 0 5px #aaa;
  25. }
  26. body form > div {
  27. height: 36px;
  28. display: flex;
  29. justify-content: space-between;
  30. align-items: center;
  31. }
  32. body form div:last-of-type {
  33. display: flex;
  34. justify-content: center;
  35. }
  36. body form input {
  37. border: none;
  38. outline: none;
  39. padding-left: 5px;
  40. height: 20px;
  41. }
  42. body form input:hover {
  43. box-shadow: 0 0 5px #aaa;
  44. }
  45. body form button {
  46. flex: auto;
  47. height: 30px;
  48. background-color: green;
  49. color: white;
  50. border: none;
  51. outline: none;
  52. }
  53. body form button:hover {
  54. background-color: lightcoral;
  55. cursor: pointer;
  56. box-shadow: 0 0 5px #aaa;
  57. }
  58. body a {
  59. color: #888;
  60. text-decoration: none;
  61. margin-top: 15px;
  62. }
  63. body a:hover {
  64. color: lightcoral;
  65. font-weight: bold;
  66. }

handle.php

  1. <?php
  2. /**
  3. * 1.开启会话
  4. * 2.查看数据
  5. */
  6. session_start();
  7. $db = new PDO('mysql:host=localhost;dbname=phpedu','root','901026yk');
  8. $stmt = $db->prepare('select * from user');
  9. $stmt->execute();
  10. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  11. $action = $_GET['action'];
  12. /**
  13. *
  14. */
  15. switch ($action)
  16. {
  17. case 'login':
  18. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  19. $password = sha1($_POST['password']);
  20. $email = $_POST['email'];
  21. $result = array_filter($users, function ($user) use ($password, $email) {
  22. return $user['password'] == $password && $user['email'] == $email;
  23. });
  24. if (count($result) == 1) {
  25. $_SESSION['user'] = serialize(array_pop($result));
  26. exit('<script>alert("登录成功");location.href="index.php"</script>');
  27. } else {
  28. exit('请求类型错误');
  29. }
  30. }
  31. case 'logout':
  32. if (isset($_SESSION['user'])) {
  33. session_destroy();
  34. exit('<script>alert("退出成功");location.href="index.php"</script>');
  35. }
  36. case 'register':
  37. $email= $_POST['email'];
  38. $name= $_POST['name'];
  39. $password= sha1($_POST['p2']);
  40. $register_time = time();
  41. // 2. sql
  42. $sql = <<<SQL
  43. INSERT `user`
  44. SET `name`= ?,
  45. `email`= ?,
  46. `password`= ?,
  47. `register_time`= ?;
  48. SQL;
  49. $stmt = $db->prepare($sql);
  50. $data = [$name,$email,$password, $register_time];
  51. if ($stmt->execute($data)) {
  52. if ($stmt->rowCount() > 0) {
  53. // 注册成功之后,让用户自动登录
  54. $sql='SELECT * FROM `user` WHERE `id` = ' . $db->lastInsertId();
  55. $stmt = $db->prepare($sql);
  56. $stmt->execute();
  57. $newUser =$stmt->fetch(PDO::FETCH_ASSOC);
  58. $_SESSION['user'] = serialize($newUser);
  59. exit('<script>alert("注册成功");location.href="index.php"</script>');
  60. } else {
  61. exit('<script>alert("注册失败");location.href="register.php"</script>');
  62. }
  63. } else {
  64. print_r($stmt->errorInfo());
  65. }
  66. // no break
  67. default:
  68. exit('参数非法或未定义操作');
  69. }
  70. /**
  71. * serialize:序列化数据
  72. */

index.php

  1. <?php
  2. namespace _0822;
  3. session_start();
  4. // 判断是否已登录?
  5. if (isset($_SESSION['user'])) {
  6. $user = unserialize($_SESSION['user']);
  7. }
  8. ?>
  9. <!DOCTYPE html>
  10. <html lang="en">
  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. <link rel="stylesheet" href="CSS/index.css">
  17. <style>
  18. </style>
  19. </head>
  20. <body>
  21. <nav>
  22. <a href="index.php">我的博客</a>
  23. <?php if (isset($user)) : ?>
  24. <span style="margin-left: 300px"><?=$user['name']?></span>
  25. <a id="logout">退出</a>
  26. <?php else: ?>
  27. <a href="login.php">登录</a>
  28. <?php endif ?>
  29. </nav>
  30. <script>
  31. // 为退出按钮创建事件监听器
  32. document.querySelector('#logout').addEventListener('click', function(event) {
  33. if (confirm('是否退出')) {
  34. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  35. event.preventDefault();
  36. // 跳转到退出事件处理器
  37. window.location.assign('handle.php?action=logout');
  38. }
  39. },true);
  40. </script>
  41. </body>
  42. </html>

login.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户登录</title>
  6. <link rel="stylesheet" type="text/css" href="css/style.css">
  7. </head>
  8. <body>
  9. <h3>用户登录</h3>
  10. <form action="handle.php?action=login" method="post">
  11. <div>
  12. <label for="email">邮箱:</label>
  13. <input type="email" name="email" id="email" placeholder="demo@email.com" required autofocus>
  14. </div>
  15. <div>
  16. <label for="password">密码:</label>
  17. <input type="password" name="password" id="password" placeholder="不少于6位" required>
  18. </div>
  19. <div>
  20. <button>提交</button>
  21. </div>
  22. </form>
  23. <a href="register.php">还没有帐号, 注册一个吧</a>
  24. </body>
  25. </html>

register.php
```php
<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="CSS/style.css">
<title>注册用户</title>
</head>

<body>
<h3>用户注册</h3>
<form action="handle.php?action=register" method="post" onsubmit="return compare()">
<div>
<label for="name">呢称:</label>
<input type="text" name="name" id="name" placeholder="不少于3个字符" required autofocus>
</div>
<div>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email" placeholder="demo@email.com" required>
</div>

<div>
<label for="p1">密码:</label>
<input type="password" name="p1" id="p1" placeholder="不少于6位" required>
</div>

<div>
<label for="p2">重复:</label>
<input type="password" name="p2" id="p2" placeholder="必须与上面一致" required>
</div>

<div>
<button>提交</button><span id="tips" style="color: red"></span>
</div>
</form>
<a href="login.php">我有帐号,直接登录</a>

<script>
// 验证二次密码是否相等?
function compare() {
if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
document.querySelector(‘#tips’).innerText = ‘二次密码不相等’;
return false;
}
}
</script>
</body>

</html>

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