commandstring/cookies

在PHP中操作cookie的更简单方法

v6.0.0 2023-12-02 21:02 UTC

This package is auto-updated.

Last update: 2024-10-01 00:14:54 UTC


README

使用Composer安装:composer require commandstring/cookies

要求

  • PHP >=8.0
  • 基本的PHP OOP理解
  • Composer 2

基本用法

require  __DIR__."/vendor/autoload.php";
use CommandString\Cookies\Cookie;

$cookies = new Cookie();

#                              v hours 
#                              v valid   v seconds valid
$cookies->set("name", "value", 168, 10, 30); // by default cookies expire in a week
#                                   ^ minutes valid

// After page refresh //
echo $cookies->get("name"); // output: value

// Delete cookie //
$cookie->delete("name"); // remove the cookie

// Delete all cookies
$cookie->deleteAll();

// Check if a cookie exists
$cookie->exists("name"); // returns bool

使用CommandString/Encrypt与CommandString/Cookies结合使用

建议查看CommandString/Encrypt的README

use CommandString\CookieEncryption\CookieEncryption;
use CommandString\Cookies\Cookie;

// use the cookieEncryption class that wraps around cmdstr/encrypt/encryption class
$cookies = new Cookie(new CookieEncryption("MZCdg02STLzrsj05KE3SIL62SSlh2Ij", "AES-256-CTR"));
// ... now cmdstr/encrypt will handle encrypting cookies