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
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @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
|
|
|
|
* @param boolean $httpOnly See http://php.net/set_session (PHP 5.2+ only)
|
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();
|
2010-10-04 06:40:50 +02:00
|
|
|
|
2010-04-12 03:43:32 +02:00
|
|
|
// Versions of PHP prior to 5.2 do not support the $httpOnly value
|
2010-10-04 06:40:50 +02:00
|
|
|
if(version_compare(phpversion(), 5.2, '<')) {
|
2010-04-12 03:43:32 +02:00
|
|
|
setcookie($name, $value, $expiry, $path, $domain, $secure);
|
2010-10-04 06:40:50 +02:00
|
|
|
} else {
|
2010-04-12 03:43:32 +02:00
|
|
|
setcookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
|
2010-10-04 06:40:50 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2011-12-17 02:21:09 +01:00
|
|
|
if(self::$report_errors)
|
|
|
|
suser_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2011-12-17 02:21:09 +01:00
|
|
|
}
|