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.
|
2011-12-17 02:21:09 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage misc
|
|
|
|
*/
|
2010-05-25 06:19:57 +02:00
|
|
|
class Cookie {
|
2011-12-17 02:21:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
static $report_errors = true;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Set a cookie variable
|
2009-09-10 05:22:50 +02:00
|
|
|
*
|
|
|
|
* @param string $name The variable name
|
|
|
|
* @param string $value The variable value. May be an array or object if you wish.
|
2011-12-17 02:21:09 +01:00
|
|
|
* @param int $expiry The expiry time, in days. Defaults to 90.
|
2009-09-10 05:22:50 +02:00
|
|
|
* @param string $path See http://php.net/set_session
|
|
|
|
* @param string $domain See http://php.net/set_session
|
|
|
|
* @param boolean $secure See http://php.net/set_session
|
2012-03-31 09:08:54 +02:00
|
|
|
* @param boolean $httpOnly See http://php.net/set_session
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2011-12-17 02:21:09 +01:00
|
|
|
static function set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!headers_sent($file, $line)) {
|
2011-12-17 02:21:09 +01:00
|
|
|
$expiry = $expiry > 0 ? time()+(86400*$expiry) : $expiry;
|
2009-09-10 05:22:50 +02:00
|
|
|
$path = ($path) ? $path : Director::baseURL();
|
2012-03-31 09:08:54 +02:00
|
|
|
setcookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2012-03-31 09:08:54 +02:00
|
|
|
if(self::$report_errors) {
|
2012-02-16 02:59:56 +01:00
|
|
|
user_error("Cookie '$name' can't be set. The site started outputting was content at line $line in $file", E_USER_WARNING);
|
2012-03-31 09:08:54 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a cookie variable
|
|
|
|
*/
|
|
|
|
static function get($name) {
|
|
|
|
return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
|
|
|
|
}
|
|
|
|
|
2011-12-17 02:21:09 +01:00
|
|
|
static function forceExpiry($name, $path = null, $domain = null) {
|
2008-04-26 08:31:36 +02:00
|
|
|
if(!headers_sent($file, $line)) {
|
2011-12-17 02:21:09 +01:00
|
|
|
self::set($name, null, -20, $path, $domain);
|
2008-04-26 08:31:36 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
2011-12-17 02:21:09 +01:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
static function report_errors() {
|
|
|
|
return self::$report_errors;
|
|
|
|
}
|
2012-02-16 02:59:56 +01:00
|
|
|
}
|