2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* A set of static methods for manipulating cookies.
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage misc
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Cookie extends Object {
|
2008-11-22 04:33:00 +01:00
|
|
|
static $report_errors = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set a cookie variable
|
|
|
|
* @param name The variable name
|
|
|
|
* @param value The variable value. May be an array or object if you wish.
|
|
|
|
* @param expiryDays The expiry time, in days. Defaults to 90.
|
|
|
|
*/
|
|
|
|
static function set($name, $value, $expiryDays = 90) {
|
|
|
|
if(!headers_sent($file, $line)) {
|
2008-11-22 04:33:00 +01:00
|
|
|
$expiry = $expiryDays > 0 ? time()+(86400*$expiryDays) : 0;
|
|
|
|
setcookie($name, $value, $expiry, Director::baseURL());
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2008-11-22 04:33:00 +01:00
|
|
|
if(self::$report_errors) user_error("Cookie '$name' can't be set. The site started outputting was content at line $line in $file", E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-11-22 04:33:00 +01:00
|
|
|
$_COOKIE[$name] = $value;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a cookie variable
|
|
|
|
*/
|
|
|
|
static function get($name) {
|
|
|
|
return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function forceExpiry( $name ) {
|
2008-04-26 08:31:36 +02:00
|
|
|
if(!headers_sent($file, $line)) {
|
|
|
|
setcookie( $name, null, time() - 86400 );
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-11-22 04:33:00 +01:00
|
|
|
|
|
|
|
static function set_report_errors($reportErrors) {
|
|
|
|
self::$report_errors = $reportErrors;
|
|
|
|
}
|
|
|
|
static function report_errors() {
|
|
|
|
return self::$report_errors;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|