安全开发-PHP应用&后台模块&Session&Cookie&Token&身份验证&唯一性

 目录

一:cookie

1.cookie的作用:判断用户的身份

 2.输入用户名和密码判断正确登录成功后会得到cookie值

3.拿到cookie后再浏览器存储中加入cookie值便能绕过登录界面访问登录成功首页

二:session 

 三:token


一:cookie
1.cookie的作用:判断用户的身份

<!DOCTYPE html>
<html lang="en">
<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;
            background-color: #f1f1f1;
        }

        .container {
            width: 300px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
            text-align: center;
        }

        h2 {
            margin-bottom: 20px;
            color: #333;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <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>
</body>
</html>

<?php

include '../config.php';
//登录文件
$name=@$_POST[username];
$pass=@$_POST[password];

$sql="select * from admin where username='$name' and password='$pass'";
$data=mysqli_query($con,$sql);
if(mysqli_num_rows($data)) {
   // echo "<script>alert('恭喜登录成功')</script>script>";
    $expire=time()+60*60*24*30;//设置一个月过期
    setcookie('username',$name,$expire,'/');
    setcookie('password',$pass,$expire,'/');
    header('Location:index-c.php');
}else{
    echo 'no ok';
}
?>

登录窗口 

<?php
//登录成功首页
if($_COOKIE['username']=='admin' and $_COOKIE['password']='123456'){

}else{ echo "用户名或密码错误";
    header('Location:admin-c.php');

}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>后台首页</title>

</head>
<body>
<h1>
    后台首页
</h1>
<p>欢迎你,<?php echo $_COOKIE['username']; ?>!</p>
<p><a href="logout-c.php">退出</a></p>
</body>
</html>

<?php

//退出
setcookie('username','',time(),'/');//删除cookie,将cookie值置空
setcookie('password','',time(),'/');
header('Location:admin-c.php');
exit;
?>

 2.输入用户名和密码判断正确登录成功后会得到cookie值

 

3.拿到cookie后再浏览器存储中加入cookie值便能绕过登录界面访问登录成功首页

 

二:session 

session:存储在服务端,更安全;在浏览器关闭后过期

与cookie代码类似,session文件存储位置:\phpstudy\phpstudy_pro\Extensions\tmp\tmp

<?php
//登录文件,采用session验证
include '../config.php';
//登录文件
$name=@$_POST['username'];
$pass=@$_POST['password'];
$sql="select * from admin where username='$name' and password='$pass';";
$data=mysqli_query($con,$sql);
if($_SERVER["REQUEST_METHOD"]=="POST"){
    if(mysqli_num_rows($data)>0){
        session_start();
        $_SESSION['username']=$name;
        $_SESSION['password']=$pass;
        header('Location:index-s.php');
        exit();
    } else {
        echo '<script>alert("错误")</script>';
        //header('Location:admin-s.php');
    }

}

 三:token

token:判断数据包的唯一性,随机产生,防止暴力攻击;随机产生的token与session[token]比较,一致才能登录成功,在网站上登录session[token]会自动刷新。而抓包后暴力破解用户密码,session[token]不会改变,token一直改变不正确登录不成功。

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__落樱__@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值