Newwebshop/classes/Cookie.php

57 lines
1.3 KiB
PHP

<?php
/**
* Copyright seit 2024 Webshop System
*
* Cookie-Verwaltung für das Webshop-System
*
* @author Webshop System
* @license GPL v3
*/
class Cookie
{
protected $name;
protected $expire;
protected $domain;
protected $secure;
protected $samesite;
protected $data = [];
public function __construct($name, $path = '', $expire = 0, $domain = null, $secure = false, $samesite = 'Lax')
{
$this->name = $name;
$this->expire = $expire;
$this->domain = $domain;
$this->secure = $secure;
$this->samesite = $samesite;
if (isset($_COOKIE[$name])) {
$this->data = json_decode($_COOKIE[$name], true) ?: [];
}
}
public function set($key, $value)
{
$this->data[$key] = $value;
$this->save();
}
public function get($key)
{
return $this->data[$key] ?? null;
}
public function save()
{
setcookie(
$this->name,
json_encode($this->data),
[
'expires' => $this->expire,
'path' => '/',
'domain' => $this->domain,
'secure' => $this->secure,
'samesite' => $this->samesite
]
);
}
}