marcoschwepp / cookie-manager

轻松管理PHP Cookie

v1.0.0 2022-10-13 19:26 UTC

This package is auto-updated.

Last update: 2024-09-13 23:56:49 UTC


README

使用PHP轻松管理Cookie

要求

  • PHP >= 7.4

安装

运行

composer require marcoschwepp/CookieManager

使用

创建Cookie

创建新的Cookie对象有两种方法。

  • 创建Cookie类的实例
// only the name is required.
$cookie = new marcoschwepp\Cookie\Cookie('testCookie');

// optional parameters: 
$cookie->setValue('123456');
$cookie->setExpiresAt(new \DateTimeImmutable()) // e.g. timestamp now + 24h = \time() + 86400
$cookie->setPath('/');
$cookie->setDomain('.local.de');
$cookie->setSecure(false);
$cookie->setHttpOnly(false);
  • 从选项数组中使用静态方法创建
$options = [
    'name' => 'Test-Cookie',
    'value' => 'Test-Value',
    'expiresAt' => new \DateTimeImmutable,
    'path' => '/',
    'domain' => 'local.de',
    'secure' => true,
    'httpOnly' => true,
];

$cookie = marcoschwepp\Cookie\Cookie::constructFromOptions($options);

保存、删除、读取

$cookie->save();
$cookie->delete('testCookie');
$cookie->load();

贡献

所有贡献都受欢迎!如果您想做出贡献,请先创建一个问题,以便您的功能、问题或疑问可以讨论。

许可证

该项目根据MIT许可证的条款进行许可。