php - 未设置 Cookie,但它们出现在 PHP 标头中
问题描述
我正在为我的应用程序创建 rest api。前端在 localhost:3001 上,后端是 PHP Slim 在 localhost:8000 上。我正在尝试设置 cookie 以响应注册请求。我正在使用https://github.com/dflydev/dflydev-fig-cookies作为 cookie,这就是代码的样子:
public function postSignUp(Request $request, Response $response)
{
$body = $request->getParsedBody();
/**
* code for creating user
*/
$result = $this->auth->authenticate($body['email'], $body['password']);
$token = $this->auth->generateAccessToken($this->auth->user);
$refreshToken = $this->auth->generateRefreshToken();
$response = $response->withStatus(201)->withJson(['message' => 'success']);
$response = FigResponseCookies::set($response, SetCookie::create('refresh_token')
->withValue($refreshToken['refresh_token'])
->withDomain('localhost')
->withPath('/')
->withExpires($refreshToken['expiration'])
->withHttpOnly());
return $response;
}
这是我在客户端获得的标题:
Response Headers:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: POST,OPTIONS
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Type: application/json
Date: Thu, 28 May 2020 14:25:33 GMT
Server: nginx/1.17.10
Set-Cookie: refresh_token=5ecfc9dd79a504.09760437; Domain=localhost; Path=/; Expires=Thu, 11 Jun 2020 14:25:33 GMT; HttpOnly
Transfer-Encoding: chunked
X-Powered-By: PHP/7.4.4
Request Headers:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,ru-UA;q=0.8,ru;q=0.7,zh-CN;q=0.6,zh;q=0.5,en-US;q=0.4,uk;q=0.3
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 96
Content-Type: application/json
Host: localhost:8000
Origin: http://localhost:3001
Pragma: no-cache
Referer: http://localhost:3001/sign-up
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
如您所见,有Set-Cookie: refresh_token=5ecfc9dd79a504.09760437; Domain=localhost; Path=/; Expires=Thu, 11 Jun 2020 14:25:33 GMT; HttpOnly
标题,但在选项卡应用程序 cookie 中什么也没有出现。
解决方案
推荐阅读
- java - Android视图模型类没有零参数构造函数,片段中的实例化异常
- python - 如何将保存在表单中的用户变量传递给 Django 中的按钮链接
- javascript - 如何根据下拉选择填充强类型文本框
- firebase - 为什么提供类“QueryDocumentSnapshot”的 Firestore 数据没有实例方法“[]”。错误?
- python - 路径('',views.index,name='index'),AttributeError:模块'learning_logs.views'没有属性'index'
- python - 如何在 Matplotlib 中绘制一个非常大的数据集(日期、时间(x 轴)与记录的值(y 轴),每天每 15 分钟一次,持续一年)
- javascript - 如何将 JSON 数据从前端 JavaScript 发送到使用 Python Flask 构建的后端服务器
- shell - 从文件 1 中删除与文件 2 中特定列中的元素匹配的行
- javascript - 无法将反应应用程序部署到 github 页面
- r - 如何消除 ggplot 图例中的空白?