Blogger Information
Blog 24
fans 4
comment 0
visits 20138
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1月14日 学号:478291 cookie与session
Lin__
Original
650 people have browsed it
  • 数据保存在客户端浏览器上,如果浏览器关闭cookie,则无法使用
  • 创建cookie:setcookie(名称,值,[过期时间])
  • 使用cookie:$_COOKIE['名称']
  • 删除cookie:为cookie设置一个已经过期的时间,如:setcookie(名称,值,time()-1)
  • 使用cookie完成用户登录:

index.php

  1. <?php include 'config.php'; ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title></title>
  7. <link rel="stylesheet" href="css/public-header.css">
  8. <link rel="stylesheet" href="css/public_style.css">
  9. </head>
  10. <body>
  11. <!-- 头部 -->
  12. <div class="public-header">
  13. <!-- 左侧导航 -->
  14. <div class="left-link">
  15. <?php foreach($nav as $item): ?>
  16. <a href="<?php echo $item['url']?>"><?php echo $item['name']; ?></a>
  17. <?php endforeach; ?>
  18. </div>
  19. <!-- 左侧导航 end -->
  20. <!-- 右侧按钮 -->
  21. <div class="right-link">
  22. <?php if(!isset($_COOKIE['user'])): ?>
  23. <!-- 用户登录 -->
  24. <a href="login.php">
  25. <i class="iconfont icon-huiyuan2"></i>用户登录
  26. </a>
  27. <!-- 免费注册 -->
  28. <a href="#">免费注册</a>
  29. <?php else: ?>
  30. <a href="#"><?php echo $_COOKIE['user']; ?></a>
  31. <a href="handle.php?action=logout">退出登录</a>
  32. <?php endif; ?>
  33. </div>
  34. <!-- 右侧按钮 end -->
  35. </div>
  36. <!-- 头部 end -->
  37. </body>
  38. </html>

login.php

  1. <?php
  2. if(filter_has_var(INPUT_COOKIE,'user')){
  3. die('已登录,请勿重复登录');
  4. }
  5. ?>
  6. <!doctype html>
  7. <html>
  8. <head>
  9. <meta charset="utf-8">
  10. <title>用户登录</title>
  11. <style>
  12. form{
  13. width: 400px;
  14. height: 200px;
  15. background-color: #0cbadf;
  16. border-radius: 5px;
  17. text-align: center;
  18. margin: auto;
  19. }
  20. form > div{
  21. display: grid;
  22. grid-template-columns: 100px 200px;
  23. margin: 20px 0;
  24. grid-column-gap:10px;
  25. }
  26. form > div > label{
  27. text-align: right;
  28. }
  29. form > div > input{
  30. border-radius: 5px;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <form action="handle.php?action=login" method="post">
  36. <h3>用户登录</h3>
  37. <div>
  38. <label for="username">用户名</label>
  39. <input type="text" name="username" id="username" required autofocus>
  40. </div>
  41. <div>
  42. <label for="password">密码</label>
  43. <input type="password" name="password" id="password" required autofocus>
  44. </div>
  45. <input type="submit" value="登录">
  46. </form>
  47. </body>
  48. </html>

handle.php

  1. <?php
  2. $user=[
  3. ['username'=>'admin','password'=>'21232f297a57a5a743894a0e4a801fc3']
  4. ];
  5. $from_page=basename(filter_input(INPUT_SERVER,'HTTP_REFERER'));
  6. $pages_arr=['index.php','login.php'];
  7. if(!in_array($from_page,$pages_arr)){
  8. die('非法来源');
  9. }
  10. $action=filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
  11. switch($action){
  12. case 'login':
  13. $username=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
  14. $password=filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
  15. if(!$username){
  16. die('请输入用户名');
  17. }
  18. $i=0;
  19. foreach($user as $item){
  20. if(in_array($username,$item)){
  21. $user_password=$item['password'];
  22. break;
  23. }else{
  24. $i++;
  25. }
  26. }
  27. if($i>=count($user)){
  28. die('该用户未注册');
  29. }
  30. if(MD5($password)!=$user_password){
  31. die('密码不正确');
  32. }else{
  33. setcookie('user',$username);
  34. echo '<script>alert("登录成功!");window.location.href="index.php";</script>';
  35. }
  36. break;
  37. case 'logout':
  38. setcookie('user',$username,time()-1);
  39. echo '<script>alert("退出登录成功!");window.location.href="index.php";</script>';
  40. break;
  41. }

session

  • 数据保存在服务器上
  • 启动session:session_start()
  • 创建session:$_SESSION['名称']=值
  • 使用session:$_SESSION['名称']
  • 删除单个session:unset($_SESSION['名称'])
  • 删除所有的session:session_unset()
  • 销毁session:session_destory()

index.php

  1. <?php
  2. include 'config.php';
  3. session_start();
  4. ?>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <meta charset="utf-8">
  9. <title></title>
  10. <link rel="stylesheet" href="css/public-header.css">
  11. <link rel="stylesheet" href="css/public_style.css">
  12. </head>
  13. <body>
  14. <!-- 头部 -->
  15. <div class="public-header">
  16. <!-- 左侧导航 -->
  17. <div class="left-link">
  18. <?php foreach($nav as $item): ?>
  19. <a href="<?php echo $item['url']?>"><?php echo $item['name']; ?></a>
  20. <?php endforeach; ?>
  21. </div>
  22. <!-- 左侧导航 end -->
  23. <!-- 右侧按钮 -->
  24. <div class="right-link">
  25. <?php if(!isset($_SESSION['user'])): ?>
  26. <!-- 用户登录 -->
  27. <a href="login.php">
  28. <i class="iconfont icon-huiyuan2"></i>用户登录
  29. </a>
  30. <!-- 免费注册 -->
  31. <a href="#">免费注册</a>
  32. <?php else: ?>
  33. <a href="#"><?php echo $_SESSION['user']; ?></a>
  34. <a href="handle.php?action=logout">退出登录</a>
  35. <?php endif; ?>
  36. </div>
  37. <!-- 右侧按钮 end -->
  38. </div>
  39. <!-- 头部 end -->
  40. </body>
  41. </html>

login.php

  1. <?php
  2. session_start();
  3. if(isset($_SESSION['user'])){
  4. die('已登录,请勿重复登录');
  5. }
  6. ?>
  7. <!doctype html>
  8. <html>
  9. <head>
  10. <meta charset="utf-8">
  11. <title>用户登录</title>
  12. <style>
  13. form{
  14. width: 400px;
  15. height: 200px;
  16. background-color: #0cbadf;
  17. border-radius: 5px;
  18. text-align: center;
  19. margin: auto;
  20. }
  21. form > div{
  22. display: grid;
  23. grid-template-columns: 100px 200px;
  24. margin: 20px 0;
  25. grid-column-gap:10px;
  26. }
  27. form > div > label{
  28. text-align: right;
  29. }
  30. form > div > input{
  31. border-radius: 5px;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <form action="handle.php?action=login" method="post">
  37. <h3>用户登录</h3>
  38. <div>
  39. <label for="username">用户名</label>
  40. <input type="text" name="username" id="username" required autofocus>
  41. </div>
  42. <div>
  43. <label for="password">密码</label>
  44. <input type="password" name="password" id="password" required autofocus>
  45. </div>
  46. <input type="submit" value="登录">
  47. </form>
  48. </body>
  49. </html>

handle.php

  1. <?php
  2. session_start();
  3. $user=[
  4. ['username'=>'admin','password'=>'21232f297a57a5a743894a0e4a801fc3']
  5. ];
  6. $from_page=basename(filter_input(INPUT_SERVER,'HTTP_REFERER'));
  7. $pages_arr=['index.php','login.php'];
  8. if(!in_array($from_page,$pages_arr)){
  9. die('非法来源');
  10. }
  11. $action=filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
  12. switch($action){
  13. case 'login':
  14. $username=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
  15. $password=filter_input(INPUT_POST,'password',FILTER_SANITIZE_STRING);
  16. if(!$username){
  17. die('请输入用户名');
  18. }
  19. $i=0;
  20. foreach($user as $item){
  21. if(in_array($username,$item)){
  22. $user_password=$item['password'];
  23. break;
  24. }else{
  25. $i++;
  26. }
  27. }
  28. if($i>=count($user)){
  29. die('该用户未注册');
  30. }
  31. if(MD5($password)!=$user_password){
  32. die('密码不正确');
  33. }else{
  34. $_SESSION['user']=$username;
  35. echo '<script>alert("登录成功!");window.location.href="index.php";</script>';
  36. }
  37. break;
  38. case 'logout':
  39. unset($_SESSION['user']);
  40. echo '<script>alert("退出登录成功!");window.location.href="index.php";</script>';
  41. break;
  42. }

运行结果



Correcting teacher:天蓬老师天蓬老师

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