博客列表 >php中的Session和cookie

php中的Session和cookie

王娇
王娇原创
2020年05月12日 17:45:01702浏览

学习总结

  • session和cookie都可以保存用户的数据
  • cookie是把用户数据保存在客户端的浏览器上
  • session是把用户数据保存在服务器端的某个文件中
  • cookie如果不设置路径,默认只能同文件夹下的文件可以访问它的cookie值,如果想让整站都访问必须设置路径
  • session只要开启,就可以访问其中保存的值

1.首页index.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="css/index.css">
  7. <title>首页</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="row">
  12. <div class="header col-12">
  13. <div><a href=""><img src="images/logo.png" alt=""></a></div>
  14. <div class="nav">
  15. <div><a href="">首页</a></div>
  16. <div><a href="">商品推销</a></div>
  17. <div><a href="">商品展示</a></div>
  18. </div>
  19. <div class="slogin">
  20. <?php
  21. session_start();
  22. if(isset($_SESSION['userNc'])):
  23. ?>
  24. <div>欢迎:<span style="color: red"><?php echo $_SESSION['userNc']; ?></span></div>
  25. <div><a href="session/handle.php?action=logout" target="main">退出</a></div>
  26. <?php
  27. else:
  28. ?>
  29. <div><a href="session/login.php" target="main">session登录</a></div>
  30. <div><a href="session/register.php" target="main">session注册</a></div>
  31. <?php
  32. endif;
  33. ?>
  34. </div>
  35. <div class="clogin">
  36. <div><a href="cookie/login.php" target="main">cookie登录</a></div>
  37. <div><a href="cookie/register.php" target="main">cookie注册</a></div>
  38. </div>
  39. </div>
  40. </div>
  41. <div class="row">
  42. <div class="col-1"></div>
  43. <div class="aside col-2">
  44. <div><a href="select.php" target="main"><span>查询商品</span> </a></div>
  45. <div><a href="insert.php" target="main"><span>添加商品</span> </a></div>
  46. <div><a href="update.php" target="main"><span>修改商品</span> </a></div>
  47. <div><a href="delete.php" target="main"><span>删除商品</span> </a></div>
  48. </div>
  49. <div class="main col-8">
  50. <iframe src="select.php" frameborder="0" width="100%" height="100%" id="main" name="main"></iframe>
  51. </div>
  52. <div class="col-1"></div>
  53. </div>
  54. <div class="row">
  55. <div class="footer col-12">
  56. footer
  57. </div>
  58. </div>
  59. </div>
  60. </body>
  61. </html>

1.1首页样式表index.css

  1. @import "reset.css";
  2. .container {
  3. min-height: 600px;
  4. margin-left: auto;
  5. margin-right: auto;
  6. display: grid;
  7. gap: 5px;
  8. }
  9. /* 每行按12列的栅格布局 */
  10. .container > .row {
  11. display: grid;
  12. grid-template-columns: repeat(12, 1fr);
  13. gap: 5px;
  14. }
  15. /* 页眉 */
  16. .container > .row > .header {
  17. height: 60px;
  18. background-color: darkturquoise;
  19. display: flex;
  20. flex-flow: row nowrap;
  21. align-items: center;
  22. }
  23. .container > .row > .header div {
  24. display: flex;
  25. flex-flow: row nowrap;
  26. align-items: center;
  27. }
  28. .container > .row > .header a {
  29. height: 100%;
  30. width: 100%;
  31. padding: 0px 5px;
  32. font-size: 1.2rem;
  33. font-weight: bolder;
  34. }
  35. .container > .row > .header > .nav {
  36. margin-left: 40px;
  37. }
  38. .container > .row > .header > .nav a {
  39. padding: 0px 20px;
  40. }
  41. .container > .row > .header > .slogin {
  42. margin-left: auto;
  43. margin-right: 20px;
  44. }
  45. /* 左侧边栏 */
  46. .container > .row > .aside {
  47. background-color: #ccc;
  48. }
  49. /* 侧边栏 */
  50. .container > .row > .aside {
  51. min-height: 500px;
  52. background-color: #ccc;
  53. border-radius: 3px;
  54. display: flex;
  55. flex-flow: column nowrap;
  56. padding: 5px;
  57. }
  58. /* 侧边栏导航 */
  59. .container > .row > .aside > div {
  60. background-color: #58c4f2;
  61. height: 40px;
  62. margin: 2px 8px;
  63. border-radius: 10px;
  64. }
  65. .container > .row > .aside > div > a {
  66. width: 100%;
  67. height: 100%;
  68. display: flex;
  69. justify-content: center;
  70. align-items: center;
  71. }
  72. .container > .row > .aside > div > a > span {
  73. font-size: 1.2rem;
  74. letter-spacing: 2px;
  75. }
  76. .container > .row > .aside > div:hover {
  77. background-color: lightgreen;
  78. box-shadow: 0 0 5px #555;
  79. }
  80. /* 网页主体部分 */
  81. .container > .row > .main {
  82. background-color: #ccc;
  83. }
  84. /* 除页眉页脚的中间部分 */
  85. .container > .row:nth-of-type(2) {
  86. min-height: 600px;
  87. }
  88. /* 页脚 */
  89. .container > .row > .footer {
  90. height: 165px;
  91. background-color: darkturquoise;
  92. }
  93. .col-1 {
  94. grid-column-end: span 1;
  95. }
  96. .col-2 {
  97. grid-column-end: span 2;
  98. }
  99. .col-3 {
  100. grid-column-end: span 3;
  101. }
  102. .col-4 {
  103. grid-column-end: span 4;
  104. }
  105. .col-5 {
  106. grid-column-end: span 5;
  107. }
  108. .col-6 {
  109. grid-column-end: span 6;
  110. }
  111. .col-7 {
  112. grid-column-end: span 7;
  113. }
  114. .col-8 {
  115. grid-column-end: span 8;
  116. }
  117. .col-9 {
  118. grid-column-end: span 9;
  119. }
  120. .col-10 {
  121. grid-column-end: span 10;
  122. }
  123. .col-11 {
  124. grid-column-end: span 11;
  125. }
  126. .col-12 {
  127. grid-column-end: span 12;
  128. }
  • 首页效果展示

2.登录页面 login.php

  1. <link rel="stylesheet" href="../css/form.css">
  2. <div class="log">
  3. <form action="handle.php?action=login" method="POST">
  4. <h2>用户登录session</h2>
  5. <div>
  6. <label for="userNmae">邮箱:</label>
  7. <input type="email" name="userName" id="userName" placeholder="请输入邮箱" require autofocus>
  8. </div>
  9. <div>
  10. <label for="passWord">密码:</label>
  11. <input type="password" name="passWord" id="passWord" placeholder="请输入密码" require>
  12. </div>
  13. <div>
  14. <button type="submit">登录</button>
  15. </div>
  16. <div><a href="register.php">还没有账号,注册一个吧!</a></div>
  17. </form>
  18. </div>
  • 登录页面

3.注册页面 register.php

  1. <link rel="stylesheet" href="../css/form.css">
  2. <div class="reg">
  3. <form action="handle.php?action=regist" method="POST" onsubmit="return checkform(form)">
  4. <h2>用户注册cookie</h2>
  5. <div>
  6. <label for="userNmae">注册邮箱:</label>
  7. <input type="email" name="userName" id="userName" placeholder="angle@email.com" require autofocus>
  8. </div>
  9. <div>
  10. <label for="userNc">注册昵称:</label>
  11. <input type="text" name="userNc" id="userNc" placeholder="请输入昵称">
  12. </div>
  13. <div>
  14. <label for="pwd1">注册密码:</label>
  15. <input type="password" name="pwd1" id="pwd1" placeholder="请输入密码" require>
  16. </div>
  17. <div>
  18. <label for="pwd2">确认密码:</label>
  19. <input type="password" name="pwd2" id="pwd2" placeholder="请输入确认密码" require>
  20. </div>
  21. <div>
  22. <button type="submit">注册</button>
  23. </div>
  24. <div>
  25. <a href="login.php">我有账号,直接登录</a>
  26. </div>
  27. <script>
  28. function checkform(form) {
  29. //注册信息不能为空
  30. for (i = 0; i < form.length; i++) {
  31. if (form.elements[i].value.trim() == "") {
  32. alert("请将注册信息填写完整!");
  33. form.elements[i].focus();
  34. return false;
  35. }
  36. }
  37. // 两次密码必须相同
  38. if (form.pwd1.value.trim() !== form.pwd2.value.trim()) {
  39. alert("两次密码必须相同");
  40. form.pwd2.focus();
  41. return false;
  42. }
  43. }
  44. </script>
  45. </form>
  46. </div>
  • 注册页面

4.登录注册的样式表

  1. @import "reset.css";
  2. form {
  3. margin-left: auto;
  4. margin-right: auto;
  5. margin-top: 20px;
  6. background-color: darkcyan;
  7. border-radius: 10px;
  8. max-width: 500px;
  9. min-height: 400px;
  10. box-shadow: 0px 0px 2px #555;
  11. display: flex;
  12. flex-flow: column nowrap;
  13. justify-content: space-evenly;
  14. align-items: center;
  15. }
  16. form > h2 {
  17. font-size: 2rem;
  18. }
  19. form label {
  20. font-size: 1.5rem;
  21. font-weight: bolder;
  22. }
  23. form input {
  24. width: 150px;
  25. height: 30px;
  26. }

5.登录和注册的处理页面

  1. <?php
  2. session_start();//开启session
  3. require '../autoLoad.php';
  4. use compotents\conn\DBconn;
  5. $user = new DBconn();
  6. $table = 'tb_user';
  7. $where = '*';
  8. $data = [];
  9. $action = $_GET['action'];
  10. switch($action):
  11. //处理注册操作
  12. case 'regist':
  13. if($_SERVER['REQUEST_METHOD']==='POST'):
  14. $name = trim($_POST['userName']);
  15. $nc = trim($_POST['userNc']);
  16. $pwd = md5(trim($_POST['pwd1']));
  17. $tpwd = trim($_POST['pwd1']);
  18. $rdate = date('Y-m-d');
  19. $data = ['name'=>"$name",'nc'=>"$nc",'password'=>"$pwd",'tpassword'=>"$tpwd",'regdate'=>"$rdate"];
  20. //先判断一下用户名是否已经注册
  21. $where = "`name`='$name'";
  22. $res = $user->select($table,$where);
  23. if(count($res)):
  24. exit('<script>alert("邮箱已经注册过了,可直接登录");location.href="login.php";</script>');
  25. else:
  26. $rowCount = $user->insert($table,$data); //返回受影响的记录条数
  27. if($rowCount):
  28. exit('<script>alert("注册成功");location.href="login.php";</script>');
  29. else:
  30. exit('<script>alert("注册失败");location.href="register.php";</script>');
  31. endif;
  32. endif;
  33. else:
  34. die('请求类型错误!');
  35. endif;
  36. break;
  37. //处理登录操作
  38. case 'login':
  39. if($_SERVER['REQUEST_METHOD']==='POST'):
  40. $name = trim($_POST['userName']);
  41. $pwd = md5(trim($_POST['passWord']));
  42. $where = "`name`='$name' and `password`='$pwd'";
  43. $res = $user->select($table,$where);
  44. if(count($res)):
  45. $nc = $res[0]['nc'];
  46. $_SESSION['userNc'] = $nc;//写入session
  47. exit('<script>alert("登录成功");location.href="../select.php";</script>');
  48. else:
  49. exit('<script>alert("登录失败,用户名或密码错误");location.href="login.php";</script>');
  50. endif;
  51. else:
  52. die('请求类型错误!');
  53. endif;
  54. break;
  55. //处理退出操作
  56. case 'logout':
  57. if(isset($_SESSION['userNc'])):
  58. session_destroy();//清除session的值
  59. exit('<script>alert("退出成功");location.href="login.php";</script>');
  60. endif;
  61. break;
  62. default:
  63. echo '不存在此操作';
  64. endswitch;
  65. ?>
  1. <?php
  2. setcookie('userNc',$nc); //设置cookie
  3. ?>
  • 未登录不允许进行添加,更新和删除操作,在文件头部进行session判断
  1. <?php
  2. session_start();
  3. if(!(isset($_SESSION['userNc']))):
  4. exit('<script>alert("您还未登录,请登录后操作");location.href="session/login.php";</script>');
  5. endif;
  6. ?>
  • 未登录

  • 登录成功

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