Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpgt/cookie
Object oriented cookie handler.
https://github.com/phpgt/cookie
cookie cookiejar cookies http object-oriented php-cookie phpgt
Last synced: about 2 months ago
JSON representation
Object oriented cookie handler.
- Host: GitHub
- URL: https://github.com/phpgt/cookie
- Owner: PhpGt
- License: mit
- Created: 2017-11-05T17:24:41.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-10T13:57:56.000Z (almost 2 years ago)
- Last Synced: 2024-08-08T22:52:11.593Z (5 months ago)
- Topics: cookie, cookiejar, cookies, http, object-oriented, php-cookie, phpgt
- Language: PHP
- Homepage: https://www.php.gt/cookie
- Size: 177 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Object oriented cookie handler.
-------------------------------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
```php
// 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.