muhammetsafak / secret-cookies
通过加密cookie提供更安全的cookie存储机会。
Requires
- php: >=7.4
- initphp/encryption: ^1.0
- initphp/parameterbag: ^1.0
README
通过加密cookie提供更安全的cookie存储机会。
要求
- PHP 7.4 或更高版本
- InitPHP ParameterBag 库
- InitPHP 加密库
注意: 上述库可能有特定要求(例如 OpenSSL 和 MB_String)。
安装
composer require muhammetsafak/secret-cookies
配置
$options = [ 'algo' => 'SHA256', // String : OpenSSL Algorithm 'cipher' => 'AES-256-CTR', // String : OpenSSL Cipher 'key' => 'SecretCookie', // String : Top Secret Key 'ttl' => 3600, // Integer : Seconds - LifeTime 'path' => '/', // String 'domain' => null, // Null or String. If it is empty, it is not used. 'secure' => false, // Boolean 'httponly' => true, // Boolean 'samesite' => 'Strict', // "None", "Lax" or "Strict" ];
非常重要: 为了安全起见,必须指定 key
。否则,使用此库只是给您的服务器增加负担。用户的cookie数据将使用此密钥进行加密和解密。
使用
require_once "vendor/autoload.php"; use MuhammetSafak\SecretCookies\Segment; // See the configuration section for detailed information. $options = []; $cookie = new Segment('cookieName', $options); $cookie->set('username', 'muhammetsafak') ->set('mail', 'info@muhammetsafak.com.tr');
性能
在某些情况下,加密和解密可能成为服务器的一个巨大负担。这个库;它尝试避免每次都重复加密和解密。
通常,解密只在 __construct()
方法中执行,加密在 __destruct()
方法中执行。如果您仍然因为某种原因逃避了 __destruct()
方法;您有 save()
方法,它将通过将它们发送到用户的浏览器来使更改永久有效。
方法
has()
它检查是否在当前段中使用了当前密钥来定义数据。
public function has(string $key): bool;
get()
返回指定键的值。否则返回给定的默认值。
public function get(string $key, $default = null): mixed;
set()
定义指定键的值。
public function set(string $key, $value): self;
注意: 此方法更改将在之后生效,但不会直接发送到用户的浏览器。应使用 save()
方法,或者对象应正确终止,以便将更改发送到用户浏览器。为什么以及何时必须使用 save()
方法将在 性能 部分中解释。
remove()
public function remove(string $key): self;
注意: 此方法更改将在之后生效,但不会直接发送到用户的浏览器。应使用 save()
方法,或者对象应正确终止,以便将更改发送到用户浏览器。为什么以及何时必须使用 save()
方法将在 性能 部分中解释。
save()
如果有的话,它会将更改发送到用户的浏览器,使它们永久/有效。如果对象正确终止;PHP 将自动使用 __destruct()
方法运行它。
public function save(): void;
getDebug()
如果遇到已知错误,我们将将其保存在数组中。如果有的话,getDebug()
方法返回已知错误。
public function getDebug(): string[];
获取帮助
如果您有任何问题、担忧、错误报告等,请在存储库的问题跟踪器中提交问题。
贡献
对本项目的所有贡献都将根据 MIT 许可证发布。通过提交拉取请求或提交错误、问题或功能请求,您同意遵守此版权放弃声明。
- 分支它( https://github.com/muhammetsafak/secret-cookies/fork )
- 创建您的功能分支(git checkout -b my-new-feature)
- 提交您的更改(git commit -am "添加一些功能")
- 推送到分支(git push origin my-new-feature)
- 创建新的拉取请求
致谢
许可证
版权所有 © 2022 MIT 许可证