Object oriented cookie handler.
Clone or download
g105b Merge pull request #11 from PhpGt/dependabot/composer/phpunit/phpunit…
…-7.3.5

Bump phpunit/phpunit from 7.3.4 to 7.3.5
Latest commit dd30945 Sep 10, 2018

README.md

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

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