yiisoft/cookies

使用PSR-7方便地处理cookie

1.2.2 2024-04-05 07:20 UTC

This package is auto-updated.

Last update: 2024-08-31 06:36:23 UTC


README

Yii

Yii Cookies


Latest Stable Version Total Downloads Build status Code Coverage Mutation testing badge static analysis type-coverage

此包有助于在PSR-7环境中处理HTTP cookie

  • 提供了一个表示cookie的方便抽象
  • 允许一次性处理多个cookie
  • 生成并添加到响应中的Set-Cookie头部
  • 对cookie进行签名,以防止其值被篡改
  • 加密cookie,以防止其值被篡改
  • 提供用于加密和签名cookie值的PSR-15中间件

需求

  • PHP 7.4或更高版本。

安装

可以使用Composer安装此包

composer require yiisoft/cookies

一般用法

向响应中添加cookie

$cookie = (new \Yiisoft\Cookies\Cookie('cookieName', 'value'))
    ->withPath('/')
    ->withDomain('yiiframework.com')
    ->withHttpOnly(true)
    ->withSecure(true)
    ->withSameSite(\Yiisoft\Cookies\Cookie::SAME_SITE_STRICT)
    ->withMaxAge(new \DateInterval('P7D'));

$response = $cookie->addToResponse($response);

修改要发送的响应cookie

$cookies = \Yiisoft\Cookies\CookieCollection::fromResponse($response);
$cookies->expire('login');
$response = $cookies->setToResponse($response);

获取请求cookie

$cookies = \Yiisoft\Cookies\CookieCollection::fromArray($request->getCookieParams());

对cookie进行签名,以防止其值被篡改

$cookie = new \Yiisoft\Cookies\Cookie('identity', 'identityValue');

// The secret key used to sign and validate cookies.
$key = '0my1xVkjCJnD_q1yr6lUxcAdpDlTMwiU';
$signer = new \Yiisoft\Cookies\CookieSigner($key);

// Prefixes unique hash based on the value of the cookie and a secret key.
$signedCookie = $signer->sign($cookie);

// Validates and get backs the cookie with clean value.
$cookie = $signer->validate($signedCookie);

// Before validation, check if the cookie is signed.
if ($signer->isSigned($cookie)) {
    $cookie = $signer->validate($cookie);
}

加密cookie,以防止其值被篡改

$cookie = new \Yiisoft\Cookies\Cookie('identity', 'identityValue');

// The secret key used to sign and validate cookies.
$key = '0my1xVkjCJnD_q1yr6lUxcAdpDlTMwiU';
$encryptor = new \Yiisoft\Cookies\CookieEncryptor($key);

// Encrypts cookie value based on the secret key.
$encryptedCookie = $encryptor->encrypt($cookie);

// Validates, decrypts and get backs the cookie with clean value.
$cookie = $encryptor->decrypt($encryptedCookie);

// Before decryption, check if the cookie is encrypted.
if ($encryptor->isEncrypted($cookie)) {
    $cookie = $encryptor->decrypt($cookie);
}

使用PSR-15中间件加密和签名cookie值。

/**
 * @var \Psr\Http\Message\ServerRequestInterface $request
 * @var \Psr\Http\Server\RequestHandlerInterface $handler
 * @var \Psr\Log\LoggerInterface $logger
 */

// The secret key used to sign and validate cookies.
$key = '0my1xVkjCJnD_q1yr6lUxcAdpDlTMwiU';
$signer = new \Yiisoft\Cookies\CookieSigner($key);
$encryptor = new \Yiisoft\Cookies\CookieEncryptor($key);

$cookiesSettings = [
    'identity' => \Yiisoft\Cookies\CookieMiddleware::ENCRYPT,
    'name_[1-9]' => \Yiisoft\Cookies\CookieMiddleware::SIGN,
    'prefix*' => \Yiisoft\Cookies\CookieMiddleware::SIGN,
];

$middleware = new \Yiisoft\Cookies\CookieMiddleware(
    $logger
    $encryptor,
    $signer,
    $cookiesSettings,
);

// The cookie parameter values from the request are decrypted/validated.
// The cookie values are encrypted/signed, and appended to the response.
$response = $middleware->process($request, $handler);

创建带有未编码原始值的cookie

$cookie = (new \Yiisoft\Cookies\Cookie('cookieName'))
    ->withRawValue('ebaKUq90PhiHck_MR7st-E1SxhbYWiTsLo82mCTbNuAh7rgflx5LVsYfJJseyQCrODuVcJkTSYhm1WKte-l5lQ==')

文档

如果您需要帮助或有疑问,可以在Yii论坛找到。您还可以查看其他Yii社区资源

许可

Yii Cookies是免费软件。它根据BSD许可证的条款发布。有关更多信息,请参阅LICENSE

Yii Software维护。

支持项目

Open Collective

关注更新

Official website Twitter Telegram Facebook Slack