写一段用cookie做登录鉴权的代码
时间: 2023-03-11 14:25:37 浏览: 80
代码如下://设置cookie
res.cookie('username', username, { maxAge: 900000, httpOnly: true });//从cookie中获取用户名
let username = req.cookies['username'];//登录鉴权
if (username) {
//登录鉴权通过
// 执行其他操作
} else {
//登录鉴权失败
//执行其他操作
}
相关问题
写一段PHP代码用cookie与session实现登录
以下是一个简单的 PHP 代码示例,使用 cookie 和 session 实现登录功能:
<?php
session_start(); // 开启 session
// 检查是否已经登录
if(isset($_SESSION['username'])) {
echo "您已经登录,欢迎回来 {$_SESSION['username']}!";
exit;
}
// 如果提交了登录表单
if(isset($_POST['username']) && isset($_POST['password'])) {
// 验证用户名和密码
$username = $_POST['username'];
$password = $_POST['password'];
if($username === 'admin' && $password === '123456') {
// 验证通过,将用户信息存储到 session 中
$_SESSION['username'] = $username;
// 如果用户选择了记住我,则将用户名和密码存储到 cookie 中
if(isset($_POST['remember']) && $_POST['remember'] === 'on') {
setcookie('username', $username, time() + 3600 * 24 * 7); // 保存 7 天
setcookie('password', $password, time() + 3600 * 24 * 7);
}
// 跳转到首页
header('Location: index.php');
exit;
} else {
echo "用户名或密码错误!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form method="post" action="">
<label>用户名:<input type="text" name="username"></label><br>
<label>密码:<input type="password" name="password"></label><br>
<label><input type="checkbox" name="remember">记住我</label><br>
<button type="submit">登录</button>
</form>
</body>
</html>
写一段mitmproxy抓取cookie的代码
以下是使用mitmproxy抓取cookie的示例代码:
```python
from mitmproxy import http
def response(flow: http.HTTPFlow) -> None:
# 过滤请求,只处理响应为200的GET请求
if flow.response.status_code == 200 and flow.request.method == 'GET':
# 获取cookie
cookie = flow.request.cookies.get('cookie_name')
# 打印cookie信息
print('Cookie:', cookie)
```
在上面的示例代码中,我们定义了一个response函数,用于处理HTTP响应。通过判断响应状态码和请求方法,我们筛选出需要处理的请求。然后,我们通过获取请求的cookie信息,并打印出来。您可以根据需要修改代码以满足您的具体需求。