Gazelle/classes/cookie.class.php

49 lines
1.5 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
/*************************************************************************|
|--------------- Cookie class --------------------------------------------|
|*************************************************************************|
This class handles cookies.
$Cookie->get(); is user provided and untrustworthy
|*************************************************************************/
/*
interface COOKIE_INTERFACE {
public function get($Key);
2013-06-18 08:00:48 +00:00
public function set($Key, $Value, $Seconds, $LimitAccess);
2011-03-28 14:21:28 +00:00
public function del($Key);
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
public function flush();
}
*/
class COOKIE /*implements COOKIE_INTERFACE*/ {
const LIMIT_ACCESS = true; //If true, blocks JS cookie API access by default (can be overridden case by case)
const PREFIX = ''; //In some cases you may desire to prefix your cookies
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
public function get($Key) {
if (!isset($_COOKIE[SELF::PREFIX.$Key])) {
return false;
}
return $_COOKIE[SELF::PREFIX.$Key];
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
//Pass the 4th optional param as false to allow JS access to the cookie
2013-06-18 08:00:48 +00:00
public function set($Key, $Value, $Seconds = 86400, $LimitAccess = SELF::LIMIT_ACCESS) {
setcookie(SELF::PREFIX.$Key, $Value, time() + $Seconds, '/', SITE_URL, $_SERVER['SERVER_PORT'] === '443', $LimitAccess, false);
2011-03-28 14:21:28 +00:00
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
public function del($Key) {
2013-06-18 08:00:48 +00:00
setcookie(SELF::PREFIX.$Key, '', time() - 24 * 3600); //3600 vs 1 second to account for potential clock desyncs
2011-03-28 14:21:28 +00:00
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
public function flush() {
$Cookies = array_keys($_COOKIE);
foreach ($Cookies as $Cookie) {
$this->del($Cookie);
}
}
}