-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCookie.php
71 lines (63 loc) · 1.36 KB
/
Cookie.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace cse\helpers;
/**
* Class Cookie
*
* @package cse\helpers
*/
class Cookie
{
const COOKIE_PATH = '/';
const COOKIE_TIMEOUT = 432000;
/**
* Set cookie by name
*
* @param string $name
* @param $value
* @param string $path
* @param int $timeout
*
* @return bool
*/
public static function set(string $name, $value, string $path = self::COOKIE_PATH, int $timeout = self::COOKIE_TIMEOUT): bool
{
$_COOKIE[$name] = $value;
return setcookie($name, (string) $value, time() + $timeout, $path);
}
/**
* Check cookie by name
*
* @param string $name
*
* @return bool
*/
public static function has(string $name): bool
{
return isset($_COOKIE[$name]);
}
/**
* Get cookie by name
*
* @param string $name
* @param null $default
*
* @return null|mixed
*/
public static function get(string $name, $default = null)
{
return self::has($name) ? $_COOKIE[$name] : $default;
}
/**
* Remove cookie by name
*
* @param string $name
*/
public static function remove(string $name): void
{
if (self::has($name)) {
self::set($name, null, self::COOKIE_PATH, -time());
unset($_COOKIE[$name]);
}
}
}