-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookie.php
More file actions
100 lines (89 loc) · 2.56 KB
/
Copy pathCookie.php
File metadata and controls
100 lines (89 loc) · 2.56 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Cleantalk\Common\Variables;
/**
* Class Cookie
* Safety handler for $_COOKIE
*
* @usage \Cleantalk\Variables\Cookie::get( $name );
*
* @package Cleantalk\Variables
*/
class Cookie extends ServerVariables
{
protected static $instance;
/**
* Gets given $_COOKIE variable and save it to memory
*
* @param $name
*
* @return mixed|string
*/
protected function getVariable($name)
{
// Return from memory. From $this->variables
if (! isset(static::$instance->variables[$name])) {
if ( isset($_COOKIE[$name]) ) {
$value = $this->getAndSanitize($_COOKIE[$name]);
} else {
$value = '';
}
// Remember for further calls
static::getInstance()->rememberVariable($name, $value);
return $value;
}
return static::$instance->variables[$name];
}
/**
* Universal method to adding cookies
* Wrapper for setcookie() Conisdering PHP version
*
* @see https://www.php.net/manual/ru/function.setcookie.php
*
* @param string $name Cookie name
* @param string $value Cookie value
* @param int $expires Expiration timestamp. 0 - expiration with session
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httponly
* @param string $samesite
*
* @return void
* @psalm-suppress PossiblyUnusedMethod
*/
public static function set(
$name,
$value = '',
$expires = 0,
$path = '',
$domain = '',
$secure = null,
$httponly = false,
$samesite = 'Lax'
) {
if (headers_sent()) {
return;
}
$secure = ! is_null($secure) ? $secure : ! in_array(Server::get('HTTPS'), ['off', '']) || Server::get('SERVER_PORT') == 443;
// For PHP 7.3+ and above
if ( version_compare(phpversion(), '7.3.0', '>=') ) {
$params = array(
'expires' => $expires,
'path' => $path,
'domain' => $domain,
'secure' => $secure,
'httponly' => $httponly,
);
if ($samesite) {
$params['samesite'] = $samesite;
}
/**
* @psalm-suppress InvalidArgument
*/
setcookie($name, $value, $params);
// For PHP 5.6 - 7.2
} else {
setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);
}
}
}