phpgt / cookie
Object oriented cookie handler.
Fund package maintenance!
PhpGt
Installs: 2 901
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 1
Open Issues: 8
Requires
- php: >=7.2
Requires (Dev)
- phpunit/phpunit: 8.*
This package is auto-updated.
Last update: 2024-11-10 17:19:30 UTC
README
This library is a simple object oriented alternative to the $_COOKIE
superglobal that can be read using the same associative array style code. The Cookie
class represents cookie data in immutable objects, meaning that the state of the request/response cookies cannot be accidentally changed by undisclosed areas of code.
Example usage
// Create a replacement for $_COOKIE. $cookie = new Gt\Cookie\CookieHandler($_COOKIE); // Access values as normal. $value = $cookie["firstVisit"]; if(isset($cookie["firstVisit"])) { // Cookie "firstVisit" exists. } if($cookie->has("firstVisit")) { // Cookie "firstVisit" exists. } else { // Create a new cookie that expires in ten days. $now = new DateTime(); $expire = new DateTime("+10 days"); $cookie->set("firstVisit", $now, $expire); } // Now you can unset the superglobal!
What's not covered?
This library does not touch on encrypting cookies. To store sensitive information across HTTP requests, use a session variable. To ensure cookies can't be read by JavaScript, use a secure HTTP-only cookie.