paragonie/cookie
Composer 安装命令:
composer require paragonie/cookie
包简介
Modern cookie management for PHP 7
README 文档
README
This is a PHP7-only fork of Delight IM's Cookie library which uses the maximum level of security by default.
This means:
- Secure is set to
TRUEunless you override it. - HTTP-Only is set to
TRUEunless you override it. - Same-Site is set to
Strictunless you override it.
Modern cookie management for PHP
Requirements
- PHP 7+
Installation
-
Install via Composer (recommended)
$ composer require paragonie/cookieInclude the Composer autoloader:
require __DIR__.'/vendor/autoload.php'; -
or
-
Install manually
- Copy the contents of the
srcdirectory to a subfolder of your project - Include the files in your code via
requireorrequire_once
- Copy the contents of the
Usage
Static method
This library provides a static method that is compatible to PHP's built-in setcookie(...) function but includes support for more recent features such as the SameSite attribute:
\ParagonIE\Cookie\Cookie::setcookie('SID', '31d4d96e407aad42'); // or \ParagonIE\Cookie\Cookie::setcookie('SID', '31d4d96e407aad42', time() + 3600, '/~rasmus/', 'example.com', true, true, 'Lax');
Builder pattern
Instances of the Cookie class let you build a cookie conveniently by setting individual properties. This class uses reasonable defaults that may differ from defaults of the setcookie function.
$cookie = new \ParagonIE\Cookie\Cookie('SID'); $cookie->setValue('31d4d96e407aad42'); $cookie->setMaxAge(60 * 60 * 24); // $cookie->setExpiryTime(time() + 60 * 60 * 24); $cookie->setPath('/~rasmus/'); $cookie->setDomain('example.com'); $cookie->setHttpOnly(true); $cookie->setSecureOnly(true); $cookie->setSameSiteRestriction('Strict'); // echo $cookie; $cookie->save();
The method calls can also be chained:
(new \ParagonIE\Cookie\Cookie('SID'))->setValue('31d4d96e407aad42')->setMaxAge(60 * 60 * 24)->setSameSiteRestriction('Strict')->save();
A cookie can later be deleted simply like this:
$cookie->delete();
Note: For the deletion to work, the cookie must have the same settings as the cookie that was originally saved. So you should remember to pass appropriate values to setPath(...), setDomain(...), setHttpOnly(...) and setSecureOnly(...) again.
Managing sessions
Using the Session class, you can start and resume sessions in a way that is compatible to PHP's built-in session_start() function, while having access to the improved cookie handling from this library as well:
// start session and have session cookie with 'lax' same-site restriction \ParagonIE\Cookie\Session::start(); // or \ParagonIE\Cookie\Session::start('Lax'); // start session and have session cookie with 'strict' same-site restriction \ParagonIE\Cookie\Session::start('Strict'); // start session and have session cookie without any same-site restriction \ParagonIE\Cookie\Session::start(null);
All three calls respect the settings from PHP's session_set_cookie_params(...) function and the configuration options session.name, session.cookie_lifetime, session.cookie_path, session.cookie_domain, session.cookie_secure, session.cookie_httponly and session.use_cookies.
Likewise, replacements for
session_regenerate_id(); // and session_regenerate_id(true);
are available via
\ParagonIE\Cookie\Session::regenerate(); // and \ParagonIE\Cookie\Session::regenerate(true);
if you want protection against session fixation attacks that comes with improved cookie handling.
Additionally, access to the current internal session ID is provided via
\ParagonIE\Cookie\Session::id();
as a replacement for
session_id();
Reading and writing session data
-
Read a value from the session (with optional default value):
$value = \ParagonIE\Cookie\Session::get($key); // or $value = \ParagonIE\Cookie\Session::get($key, $defaultValue);
-
Write a value to the session:
\ParagonIE\Cookie\Session::set($key, $value);
-
Check whether a value exists in the session:
if (\ParagonIE\Cookie\Session::has($key)) { // ... }
-
Remove a value from the session:
\ParagonIE\Cookie\Session::delete($key);
-
Read and then immediately remove a value from the session:
$value = \ParagonIE\Cookie\Session::take($key); $value = \ParagonIE\Cookie\Session::take($key, $defaultValue);
This is often useful for flash messages, e.g. in combination with the
has(...)method.
Parsing cookies
$cookieHeader = 'Set-Cookie: test=php.net; expires=Thu, 09-Jun-2016 16:30:32 GMT; Max-Age=3600; path=/~rasmus/; secure'; $cookieInstance = \ParagonIE\Cookie\Cookie::parse($cookieHeader);
Specifications
Contributing
All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.
License
This project is licensed under the terms of the MIT License.
paragonie/cookie 适用场景与选型建议
paragonie/cookie 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56.84k 次下载、GitHub Stars 达 56, 最近一次更新时间为 2016 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「cookies」 「cookie」 「csrf」 「xss」 「samesite」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 paragonie/cookie 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 paragonie/cookie 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 paragonie/cookie 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Add a configurable cookie consent banner to the website.
Use Klaro! Consent Manager in Contao CMS
Helper class to easily work with cookies.
Popup component for Nette Framework
统计信息
- 总下载量: 56.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 57
- 点击次数: 34
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-07