cookie技术
cookie就是身份验证表示,通过cookie好区分每个用户的个人数据和权限,第一次登陆之后正常的网站都会赋予一个cookie
写写一个后台界面,直接让ai去写就可以
然后自己需要的提交方式,和表单值自己修改即可
生成cookie的流程
然后再创建一个专门存储账号密码的表
如何写一个登录验证代码
这里可以看到是登录成功挑战到index-c,但是我们也可以直接访问,这就造成了为未授权访问漏洞;
通过这段代码生成个cookie
现在直接访问就不懈怠任何cookie信息
这样就可以通过携带的cookie信息判断是否是管理员
然后等出按钮就是将cookie为空、
这里就涉及到了盗取管理员cookie访问管理员界面的问题,如果知道管理员的cookie可以直接访问后台
直接手写cookie访问
这种就不是很安全
session技术
优势
在创建刚刚的三个文件,换成-s
生成的s文件就存储到服务端
登录逻辑跟之前的一样
但这就是通过判断存储的s形的内容来判断,而在cookie上只能看到他使用了是形的一段字符串
判读是cookie还是s行,看网站大小
把浏览器关闭之后是否还能登录,不能就是s形
token技术
token具有数据唯一性
每生成一个数据包都会有唯一的token值,如果token值不对,发送过去的数据包,服务端就不会处理,而是直接丢弃
这最直观的就是防止暴力破解,爆破了解的数据包重发,能改账户密码,但是预知不了下一个数据包的token值,服务端直接丢弃
token值的创建和前端传输时隐藏
hidde隐藏属性
在写一个管理员界面的校验
老师写的这个token验证还是有点问题,数据包可以重放,没有判断好token
代码
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>
</head>
<body>
<div style="text-align: center; margin-top: 100px;">
<h2>后台登录</h2>
<form action=" " method="post" style="display: inline-block; text-align: left;">
<div style="margin-bottom: 15px;">
<label for="username" style="display: block; margin-bottom: 5px;">用户名:</label>
<input type="text" id="username" name="username" required style="width: 200px; padding: 5px;">
</div>
<div style="margin-bottom: 15px;">
<label for="password" style="display: block; margin-bottom: 5px;">密码:</label>
<input type="password" id="password" name="password" required style="width: 200px; padding: 5px;">
</div>
<div style="text-align: center;">
<button type="submit" style="padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer;">登录</button>
</div>
</form>
<div style="margin-top: 15px;">
<a href="#" style="color: #4CAF50; text-decoration: none;">忘记密码?</a>
</div>
</div>
</body>
</html>
<?php
include '../config.php';
$user=@$_POST['username'];
$pass=@$_POST['password'];
$sql="select * from admin where username='$user' and password='$pass';";
$data=mysqli_query($con,$sql);
if($_SERVER['REQUEST_METHOD']=='POST'){//$_SERVER['REQUEST_METHOD']该变量中存储的是表单提交的方式。所以获取表单值
if(mysqli_num_rows($data) > 0){//mysqli_num_rows返回的行数
$expire= time()+60*60*24*30;//30天,第一个60是秒
setcookie("username",$user,$expire);
setcookie("password",$pass,$expire);
//echo "<script>alert('登录成功')</script>";
header("Location: index-c.php");
}else{
echo "<script>alert('登录失败')</script>";
}
}
index-c
php"><?php
if($_COOKIE['username']=='admin' and $_COOKIE['password']=='123456'){
}else{
header('location:admin-c.php');
}
?>
<!DOCTYPE html>
<p>欢迎您 <?php echo $_COOKIE['username']; ?>!</p>
<br>
<br>
<html lang="zh-CN">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>退出登录</title>
<div class="logout-container">
<h2>退出登录</h2>
<p>您确定要退出登录吗?退出后需要重新登录才能访问您的账户。</p>
<form action="logout-c.php" method="post">
<button type="submit" class="logout-button">退出登录</button>
</form>
</div>
</body>
</html>
logout-c
php"><?php
setcookie("username", " ", time() - 3600);
setcookie("password", " ", time() - 3600);
header("location:admin-c.php");
exit;
amdin-s
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>
</head>
<body>
<div style="text-align: center; margin-top: 100px;">
<h2>后台登录</h2>
<form action=" " method="post" style="display: inline-block; text-align: left;">
<div style="margin-bottom: 15px;">
<label for="username" style="display: block; margin-bottom: 5px;">用户名:</label>
<input type="text" id="username" name="username" required style="width: 200px; padding: 5px;">
</div>
<div style="margin-bottom: 15px;">
<label for="password" style="display: block; margin-bottom: 5px;">密码:</label>
<input type="password" id="password" name="password" required style="width: 200px; padding: 5px;">
</div>
<div style="text-align: center;">
<button type="submit" style="padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer;">登录</button>
</div>
</form>
<div style="margin-top: 15px;">
<a href="#" style="color: #4CAF50; text-decoration: none;">忘记密码?</a>
</div>
</div>
</body>
</html>
<?php
include '../config.php';
$user=@$_POST['username'];
$pass=@$_POST['password'];
$sql="select * from admin where username='$user' and password='$pass';";
$data=mysqli_query($con,$sql);
if($_SERVER['REQUEST_METHOD']=='POST'){//$_SERVER['REQUEST_METHOD']该变量中存储的是表单提交的方式。所以获取表单值
if(mysqli_num_rows($data) > 0){
session_start();
$_SESSION['username']=$user;
$_SESSION['password']=$pass;
header('location:index-s.php');
exit();
}else{
echo "<script>alert('登录失败')</script>";
}
}
index-s
php"><?php
session_start();
if ($_SESSION['username']!='admin' && $_SESSION['username']!='123456'){
header('location:admin-s.php');
}
?>
<!DOCTYPE html>
<p>欢迎您 <?php echo $_COOKIE['username']; ?>!</p>
<br>
<br>
<html lang="zh-CN">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>退出登录</title>
<div class="logout-container">
<h2>退出登录</h2>
<p>您确定要退出登录吗?退出后需要重新登录才能访问您的账户。</p>
<form action="logout-c.php" method="post">
<button type="submit" class="logout-button">退出登录</button>
</form>
</div>
</body>
</html>
token
php"><?php
session_start();
$token = bin2hex(random_bytes(16));
//生成token,随机十六位字符串
$_SESSION['token'] = $token;
//将token保存到session之中
setcookie("token", $token, time() + 3600);
//将token绑定到cookie中
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>后台登录</title>
</head>
<body>
<div style="text-align: center; margin-top: 100px;">
<h2>后台登录</h2>
<form action="token-check.php" method="post" style="display: inline-block; text-align: left;">
<input type="hidden" name="token" value="<?php echo $token; ?>">
<div style="margin-bottom: 15px;">
<label for="username" style="display: block; margin-bottom: 5px;">用户名:</label>
<input type="text" id="username" name="username" required style="width: 200px; padding: 5px;">
</div>
<div style="margin-bottom: 15px;">
<label for="password" style="display: block; margin-bottom: 5px;">密码:</label>
<input type="password" id="password" name="password" required style="width: 200px; padding: 5px;">
</div>
<div style="text-align: center;">
<button type="submit" style="padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer;">登录</button>
</div>
</form>
<div style="margin-top: 15px;">
<a href="#" style="color: #4CAF50; text-decoration: none;">忘记密码?</a>
</div>
</div>
</body>
</html>
jian
kang
ping
an