当前位置: 首页 > news >正文

【小迪安全2023】day23 PHP应用后台模块SessionCookietoken身份验证唯一性

有待更新。。。

第23天 PHP应用&后台模块&Session&Cookie&token&身份验证&唯一性

演示案例

后台模块-身份验证-Cookie技术

后台模块-身份验证-Seession技术

后台模块-数据唯一性-Token技术

Cookie使用

1、客户端向服务器发送HTTP请求。

2、服务器检查请求头中是否包含cookie信息。

3、如果请求头中包含cookie信息,则服务器使用该cookie来识别客户端,否则服务器 将生成一个新的cookie。

4、服务器在响应头中设置cook1e信息并将其发送回客户端。

5、客户端接收响应并将cookie保存在本地。

6、当客户端发送下一次HTTP请求时,它会将cookie信息附加到请求头中。

7、服务器收到请求并检查cookie的有效性。

8、如果cookie有效,则服务器响应请求。否则,服务器可能会要求客户端重新登录

测试代码

admin-c.php

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>后台管理系统 - 登录</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Microsoft YaHei", Arial, sans-serif; } body { height: 100vh; background: linear-gradient(135deg, #2c3e50, #3498db); display: flex; justify-content: center; align-items: center; } .login-box { width: 360px; background: #fff; padding: 40px 30px; border-radius: 8px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); } .login-box h2 { text-align: center; margin-bottom: 30px; color: #333; } .login-box input { width: 100%; height: 42px; margin-bottom: 20px; padding-left: 12px; border: 1px solid #ddd; border-radius: 4px; font-size: 14px; } .login-box input:focus { outline: none; border-color: #3498db; } .login-box button { width: 100%; height: 42px; background: #3498db; border: none; color: #fff; font-size: 16px; border-radius: 4px; cursor: pointer; } .login-box button:hover { background: #2980b9; } .login-footer { text-align: center; margin-top: 15px; font-size: 12px; color: #999; } </style> </head> <body> <div class="login-box"> <h2>后台管理系统</h2> <form action="" method="post"> <input type="text" name="username" placeholder="用户名" required> <input type="password" name="password" placeholder="密码" required> <button type="submit">登录</button> </form> <div class="login-footer"> © 2025 管理系统 </div> </div> </body> </html> <?php //登录文件 include '../config.php'; $user=$_POST['username']; $password=$_POST['password']; //连接数据库,进行查询 $sql="select * from admin where username='$user' and password='$password';"; echo '<br>'; //echo $sql; $data=mysqli_query($con,$sql); if($_SERVER['REQUEST_METHOD']=='POST') { //是否登陆成功 if(mysqli_num_rows($data)>0) { //成功登录才有cookie $expire = time()+60*60*24*30 ;//一个月后过期 setcookie('username',$user,$expire,'/'); setcookie('password',$password,$expire,'/'); // echo "<script>alert('登陆成功');</script>"; header("location:index-c.php"); }else{ echo "<script>alert('fail');</script>"; } }

index-c.php

<?php if($_COOKIE['username']=='admin' and $_COOKIE['password']=='123456') { } else { header("location:admin-c.php"); } ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>后台管理系统</title> <style> body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f4f4f4; } .header { background-color: #2c3e50; color: white; padding: 15px; text-align: center; border-radius: 5px; margin-bottom: 20px; } .logout-btn { background-color: #e74c3c; color: white; border: none; padding: 8px 16px; border-radius: 3px; cursor: pointer; float: right; margin-top: -35px; } .logout-btn:hover { background-color: #c0392b; } .content { background-color: white; padding: 20px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); } .welcome { margin-bottom: 20px; } </style> </head> <body> <div class="header"> <h1>后台首页</h1> <a href="logout-c.php">退出登录</a> <div style="clear: both;"></div> </div> <div class="content"> <div class="welcome"> <h2>欢迎使用后台管理系统</h2> <p>当前登录用户:<?php echo $_COOKIE['username']?></p> <p>登录时间:<span id="login-time"></span></p> </div> <div> <h3>系统状态</h3> <ul> <li>用户总数:128</li> <li>今日访问量:245</li> <li>系统运行正常</li> </ul> </div> </div> <script> // 显示当前时间 document.getElementById('login-time').textContent = new Date().toLocaleString(); // 退出登录功能 function logout() { if(confirm("确定要退出登录吗?")) { alert("退出登录成功,正在跳转到登录页面..."); // 在实际应用中,这里应该跳转到登录页面 // window.location.href = "login.html"; } } </script> </body> </html> <?php //登陆成功的首页文件

logout-c.php

<?php //退出登录 //1.设置cookie为空,让之前的if语句为假,达到无法登录的效果 //2.也可以调用删除函数 setcookie('username','',time()-3600,'/'); setcookie('password','',time()-3600,'/'); header("location:admin-c.php"); exit();
http://www.jsqmd.com/news/101682/

相关文章:

  • 纪念币预约自动化工具使用指南:告别手动抢购的烦恼
  • 播客批量下载终极指南:Podcast Bulk Downloader全面解析
  • 基于STM32的智能水质检测鱼缸监测系统设计与实现
  • .NET进阶——深入理解线程(1)同步异步与单线程多线程的区分
  • 【文献分享】RAMEN:剖析脐带血中 DNA 甲基化组差异的个体、累加及交互基因-环境因素作用
  • Podcast Bulk Downloader:让你的播客收藏管理更高效
  • 基于STM32的水质检测系统设计与实现
  • 节能商业照明:核心优势、关键要素及光学性能分析
  • 【科研绘图系列】R语言绘制多种类型图形(heatmap boxplot linechart)
  • 双十一手机选购指南:剖析几款旗舰机型的影像技术突破
  • 幽冥大陆(五十二)V10酒店门锁SDK TypeScript——东方仙盟筑基期
  • LobeChat日志追踪与调试技巧:快速定位异常请求
  • LobeChat代码注释自动生成实践
  • DAY38模型可视化与推理
  • Radm1n1ster Adv.Math thinking class chapter6
  • Radm1n1ster CN
  • LobeChat能否实现AI占星师?星座运势与人格特质关联分析
  • Copilot的Plan模式到底好在哪?
  • 歌词写作伙伴:LobeChat帮你押韵和分段
  • 深入探索 WebHID:Web 标准下的硬件交互实现
  • 瑞数6补环境案例(3)——吐环境脚本
  • 读捍卫隐私08智能出行
  • 如何终极解决Windows依赖管理难题?完整系统依赖修复方案
  • 合同条款审查:LobeChat标记潜在风险点
  • Shutter Encoder:视频编辑工作流的全能转换助手
  • 当时序数据不再“只是时间”:金仓数据库如何在复杂场景中拉开与 InfluxDB 的差距
  • 有线电视系统配管一键测量
  • MiniMax+LobeChat打造情感化AI对话体验
  • 导出聊天记录为PDF/Markdown:LobeChat贴心设计
  • 脱口秀段子生成:LobeChat玩转中文谐音梗