phpgt/cookie

面向对象风格的cookie处理程序。

维护者

详细信息

github.com/PhpGt/Cookie

源代码

问题

资助包维护!
PhpGt

v1.0.2 2019-07-17 20:11 UTC

README

此库是一个简单的面向对象替代品,用于替代可以通过相同关联数组样式代码读取的$_COOKIE超级全局变量。`Cookie`类以不可变对象的形式表示cookie数据,这意味着请求/响应cookie的状态不能被代码的未知区域意外更改。

Build status Code quality Code coverage Current version PHP.Gt/Cookie documentation

示例用法

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

未涵盖的内容是什么?

此库不涉及cookie加密。要跨HTTP请求存储敏感信息,请使用会话变量。为了确保cookie不能被JavaScript读取,请使用安全的仅HTTP cookie。