mmdm/sim-cookie
Composer 安装命令:
composer require mmdm/sim-cookie
包简介
A simple yet nice cookie management library
README 文档
README
A library for cookie management.
Features
- Manage your cookies
- Encrypt cookie with Crypt library(Optional)
- Manage SameSite
- Check useragent's support for SameSite
Install
composer
composer require mmdm/sim-cookie
Or you can simply download zip file from github and extract it, then put file to your project library and use it like other libraries.
Just add line below to autoload files:
require_once 'path_to_library/autoloader.php';
and you are good to go.
How to use
// to instantiate a cookie object $cookie = new Cookie(); // then use cookie methods like $cookie->get($cookie_name);
Use with Crypt library
If you need more security on cookies, use Crypt library.
// send crypt instance through cookie // constructure (dependency injection) $cookie = new Cookie($crypt); // now your cookies are safe
If you don't need some of your cookies to be secure, pass false
as last parameter of setValue of ISetCookie method.
Available methods
Cookie
parse(string $cookie_string, bool $decode = false, bool $encrypt = true): ?ISetCookie
Send a cookie string to parse it into SetCookie object or null if
it can't parse.
Note:
To decode value of parsed string, pass true to $decode parameter.
To encrypt parsed value, pass true as $encrypt parameter.
| Parameter | Type | Default |
|---|---|---|
| $cookie_string | ISetCookie | |
| $decode | bool | false |
| $encrypt | bool | true |
set(string $name): ISetCookie
Create new cookie object and can modify all inputs after set
method.
Please refer to SetCookie Available Methods for more information.
get(?string $name = null, $prefer = null)
Get cookie's value or $prefer if not defined.
Note
To get all cookies, do not send any parameter.
getAsString(?string $name = null, bool $decode = true, bool $decrypt = false): string
Get string for a cookie.
Note
To get all cookies as string, send null as $name
remove(string $name): ICookie
Remove a cookie(event from $_COOKIE variable).
has(string $name): bool
Check if there is cookie with specific name.
getCookieValueFromString(string $str)
Get(decrypted) value.
Note
It is useful when there is a cookie value is inside a header and want get actual value and check it.
$value = $cookie->getCookieValueFromString($the_cookie_value_that_is_encrypted_or_raw);
SetCookie Available Methods
getName(): ?string
Get cookie name.
setValue(?string $value, bool $encrypt = true): ISetCookie
Set value of cookie.
Note
To encrypt value pass true as second parameter.
getValue(): string
Get cookie value.
setExpiration($expire): ISetCookie
Set expiration to cookie.
getExpiration(): int
Get expiration of cookie.
setPath(?string $path): ISetCookie
Set cookie path.
getPath(): ?string
Get cookie path.
setDomain(?string $domain): ISetCookie
Set domain of cookie.
getDomain(): ?string
Get cookie domain.
setSecure(?bool $answer): ISetCookie
Set secure string to cookie.
isSecure(): ?bool
Check if secure string set or not.
setHttpOnly(?bool $answer): ISetCookie
Set httponly string to cookie.
isHttpOnly(): ?bool
Check if httponly set or not.
setSameSite(?string $same_site): ISetCookie
Set same site for modern browsers.
Note
Acceptable values are:
-
ICookie::SAME_SITE_NONE - is
Nonestring -
ICookie::SAME_SITE_LAX - is
Laxstring -
ICookie::SAME_SITE_STRICT - is
Strictstring
getSameSite(): ?string
Get samesite if set before or null.
setExtra(string $extra): ISetCookie
Set extra string for cookie (if needed and supported).
getExtra(): string
Get extra cookie string
setUseragent(?string $useragent = null): ISetCookie
Set useragent for check against to see if it support samesite or not.
Note
By default it'll get browser from $_SERVER.
getUseragent(): ?string
Get useragent.
Note
This method will return the useragent you set not useragent from
$_SERVER variable.
toString(bool $decode = true, bool $decrypt = false): string
Get cookie header of this cookie as string
Example of output:
"Set-Cookie: tmp-cookie=A simple cookie; expires=Sat, 03-Apr-2021 10:45:55 GMT; Max-Age=60" "Set-Cookie: tmp-cookie=A simple cookie number 2; expires=Sat, 03-Apr-2021 10:45:55 GMT; Max-Age=60; secure; SameSite=None"
save(bool $encode = true): bool
Save configured cookie. $encode will encode value.
Note
You MUST call this method to save cookie otherwise you missed your cookie.
Dependencies
There is just one dependency and it is Crypt library. With this feature, if any cookie hijacking happens, they can't see actual data because it is encrypted.
License
Under MIT license.
mmdm/sim-cookie 适用场景与选型建议
mmdm/sim-cookie 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 08 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mmdm/sim-cookie 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mmdm/sim-cookie 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 59
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-19