mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
Basic cookie jar implementation
This commit is contained in:
parent
a7d25081cd
commit
5365e39fc2
|
|
@ -26,4 +26,52 @@ namespace phpOMS\Cookie;
|
|||
*/
|
||||
class CookieJar
|
||||
{
|
||||
private $cookies = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->cookies = $_COOKIE;
|
||||
}
|
||||
|
||||
public function set($id, $value, \int $expiry = 86400, $path = '/', $domain = null, \bool $secure = false, \bool $httponly = true, \bool $overwrite = true) : \bool
|
||||
{
|
||||
if ($overwrite || !isset($this->cookies[$id])) {
|
||||
$this->cookies[$id] = [
|
||||
'value' => $value,
|
||||
'expiry' => $expiry,
|
||||
'path' => $path,
|
||||
'domain' => $domain,
|
||||
'secure' => $secure,
|
||||
'httponly' => $httponly,
|
||||
];
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function remove($id) : \bool
|
||||
{
|
||||
if (isset($this->cookies[$id])) {
|
||||
unset($this->cookies[$id]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function delete($id) : \bool
|
||||
{
|
||||
$this->remove($id);
|
||||
setcookie($id, '', time() - 3600);
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
foreach ($this->cookies as $key => $cookie) {
|
||||
setcookie($key, $cookie['value'], $cookie['expiry'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user