phpgt/cookie

Object oriented cookie handler.

Maintainers

Details

github.com/PhpGt/Cookie

Source

Issues

Fund package maintenance!
PhpGt

v1.0.2 2019-07-17 20:11 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.

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.