36 public static function set($name, $value, $duration = 60 * 60 * 24, $path =
'/', $secure =
false, $httpOnly =
false): bool {
38 $value = self::$transformer->transform ( $value );
40 return \setcookie ( $name, $value, $duration ? (\time () + $duration) : 0, $path, $secure, $httpOnly );
50 public static function get($name, $default =
null) {
51 $v = $_COOKIE [$name] ?? $default;
52 if ($v!=
null && self::$useTransformer && isset ( self::$transformer )) {
53 return self::$transformer->reverse ( rawurldecode($v) );
64 public static function delete($name, $path =
'/'): bool {
65 if (isset ( $_COOKIE [$name] )) {
66 unset ( $_COOKIE [$name] );
68 return \setcookie ( $name,
'', \time () - 3600, $path );
74 public static function deleteAll($path =
'/'): void {
75 foreach ( $_COOKIE as $name => $_ ) {
76 self::delete ( $name, $path );
87 public static function exists($name): bool {
88 return isset ( $_COOKIE [$name] );
103 public static function setRaw($name, $value, $duration = 60 * 60 * 24, $path =
'/', $secure =
false, $httpOnly =
false): bool {
104 if ($value!=null && self::$useTransformer && isset ( self::$transformer )) {
105 $value = self::$transformer->transform ( $value );
107 return \setrawcookie ( $name, $value, \time () + $duration, $path, $secure, $httpOnly );
117 public static function filter(
string $key,
int $filter=\FILTER_DEFAULT,$default=
null){
118 return \filter_input(INPUT_COOKIE,$key,$filter)??$default;
122 self::$transformer = $transformer;
123 self::$useTransformer =
true;
127 if (isset ( self::$transformer )) {
128 return \get_class ( self::$transformer );
Http Cookies utilities Ubiquity\utils\http$UCookie This class is part of Ubiquity.
static getTransformerClass()
static exists($name)
Tests the existence of a cookie.
static setTransformer(TransformerInterface $transformer)
static filter(string $key, int $filter=\FILTER_DEFAULT, $default=null)
Gets a specific cookie by name and optionally filters it.
static setRaw($name, $value, $duration=60 *60 *24, $path='/', $secure=false, $httpOnly=false)
Sends a raw cookie without urlencoding the cookie value.
static deleteAll($path='/')
Deletes all cookies.