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
|
|
|
|
*/
|
2010-05-25 06:19:57 +02:00
|
|
|
class Cookie {
|
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.
|
|
|
|
* @param int $expiryDays The expiry time, in days. Defaults to 90.
|
|
|
|
* @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
|
|
|
*/
|
2009-09-10 05:22:50 +02:00
|
|
|
static function set($name, $value, $expiryDays = 90, $path = null, $domain = null, $secure = false, $httpOnly = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!headers_sent($file, $line)) {
|
2008-11-22 04:33:00 +01:00
|
|
|
$expiry = $expiryDays > 0 ? time()+(86400*$expiryDays) : 0;
|
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 {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
2010-10-04 06:40:50 +02:00
|
|
|
?>
|