Servus,
ich hab mal vor längerer Zeit eine Klasse für Cookies geschrieben.
Wollte es grad testen, funktioniert aber nur bedingt.
Unter Firefox wird das Cookie normal gesetzt, unter Chrome jedoch nicht.
Dies funktioniert jedoch einwandfrei:
Klasse:
Jemand ne Idee woran es liegt?
ich hab mal vor längerer Zeit eine Klasse für Cookies geschrieben.
Wollte es grad testen, funktioniert aber nur bedingt.
Unter Firefox wird das Cookie normal gesetzt, unter Chrome jedoch nicht.
Dies funktioniert jedoch einwandfrei:
Code:
setcookie ("test", "value");
Code:
<?php
class SecureCookie
{
private $name;
private $secret;
public function __construct($name, $secret)
{
$this->name = $name;
$this->secret = $secret;
}
private function Generate($id, $expiration)
{
$key = hash_hmac('sha512', $id . $expiration, $this->secret);
$hash = hash_hmac('sha512', $id . $expiration, $key);
$cookie = $id . '|' . $expiration . '|' . $hash;
return $cookie;
}
public function Verify()
{
if(!empty($_COOKIE[$this->name])) {
list($id, $expiration, $hmac) = explode('|', $_COOKIE[$this->name]);
$key = hash_hmac('sha512', $id . $expiration, $this->secret);
$hash = hash_hmac('sha512', $id . $expiration, $key);
if ($hmac != $hash || $expiration < time()) {
$this->Destroy();
return false;
}
return true;
}
return false;
}
public function getID()
{
if (!empty($_COOKIE[$this->name])) {
list($id, $expiration, $hmac) = explode('|', $_COOKIE[$this->name]);
return $id;
}
return false;
}
public function Destroy()
{
setcookie($this->name, '', time() - 1000, '/');
}
public function setCookie($id, $remember = false)
{
if ($remember) {
$expiration = time() + 2419200; // 28 days
} else {
$expiration = time() + 43200; // 12 hours
}
$cookie = $this->Generate($id, $expiration);
setcookie($this->name, $cookie, $expiration, '/', $_SERVER['HTTP_HOST'], false, true);
}
}
?>