1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BFW\Helpers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Helpers to manage cookies |
7
|
|
|
*/ |
8
|
|
|
class Cookies |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string $path The path on the server in which the cookie will |
12
|
|
|
* be available on |
13
|
|
|
*/ |
14
|
|
|
public static $path = '/'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string $domain The (sub)domain that the cookie is available to. |
18
|
|
|
*/ |
19
|
|
|
public static $domain = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool $httpOnly When TRUE the cookie will be made accessible only |
23
|
|
|
* through the HTTP protocol. This means that the cookie won't be |
24
|
|
|
* accessible by scripting languages, such as JavaScript. |
25
|
|
|
*/ |
26
|
|
|
public static $httpOnly = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool $secure Indicates that the cookie should only be transmitted |
30
|
|
|
* over a secure HTTPS connection from the client. |
31
|
|
|
* |
32
|
|
|
* Please change the value to false if your site not use HTTPS connection. |
33
|
|
|
*/ |
34
|
|
|
public static $secure = true; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string $sameSite Same-site cookies allow servers to mitigate the |
38
|
|
|
* risk of CSRF and information leakage attacks by asserting that a |
39
|
|
|
* particular cookie should only be sent with requests initiated |
40
|
|
|
* from the same registrable domain. |
41
|
|
|
* |
42
|
|
|
* Value can only be "lax" or "strict". |
43
|
|
|
*/ |
44
|
|
|
public static $sameSite = 'lax'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a cookie |
48
|
|
|
* |
49
|
|
|
* @param string $name The cookie's name |
50
|
|
|
* @param mixed $value The cookie's value |
51
|
|
|
* @param integer $expire The cookie's expire time |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public static function create(string $name, $value, int $expire = 1209600) |
56
|
|
|
{ |
57
|
|
|
$cookieText = 'Set-Cookie: '.$name.'='.$value; |
58
|
|
|
|
59
|
|
|
$expireTime = new \DateTime; |
60
|
|
|
$expireTime->modify('+ '.$expire.' second'); |
61
|
|
|
$cookieText .= '; Expires='.$expireTime->format('D, d M Y H:i:s e'); |
62
|
|
|
|
63
|
|
|
$cookieText .= '; Path='.self::$path; |
64
|
|
|
$cookieText .= '; Domain='.self::$domain; |
65
|
|
|
|
66
|
|
|
if (self::$httpOnly === true) { |
67
|
|
|
$cookieText .= '; HttpOnly'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (self::$secure === true) { |
71
|
|
|
$cookieText .= '; Secure'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!empty(self::$sameSite)) { |
75
|
|
|
$cookieText .= '; Samesite='.self::$sameSite; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
header($cookieText); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|