Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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.

***


Build status


Code quality


Code coverage


Current version


PHP.Gt/Cookie documentation

## 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.