57 $supportsAssoc = version_compare( PHP_VERSION,
'7.3.0',
'>=' );
58 if ( $supportsAssoc ) {
59 if ( $urlEncode && version_compare( PHP_VERSION,
'7.4.3',
'>=' ) ) {
60 return setcookie( $name, $value, $options );
61 } elseif ( $urlEncode ) {
62 return setrawcookie( $name, rawurlencode( $value ), $options );
68 if ( !isset( $options[
'samesite'] ) || !strlen( $options[
'samesite'] ) ) {
71 $urlEncode ? rawurlencode( $value ) : $value,
100 $func = $urlEncode ?
'setcookie()' :
'setrawcookie()';
108 foreach ( $options as $key => $opt ) {
109 if ( $key ===
'expires' ) {
110 $expires = (int)$opt;
112 } elseif ( $key ===
'path' ) {
113 $path = (string)$opt;
115 } elseif ( $key ===
'domain' ) {
116 $domain = (string)$opt;
118 } elseif ( $key ===
'secure' ) {
119 $secure = (bool)$opt;
121 } elseif ( $key ===
'httponly' ) {
122 $httponly = (bool)$opt;
124 } elseif ( $key ===
'samesite' ) {
125 $samesite = (string)$opt;
128 $this->
error(
"$func: Unrecognized key '$key' found in the options array" );
132 if ( $found == 0 && count( $options ) > 0 ) {
133 $this->
error(
"$func: No valid options were found in the given array" );
136 if ( !strlen( $name ) ) {
137 $this->
error(
'Cookie names must not be empty' );
139 } elseif ( strpbrk( $name,
"=,; \t\r\n\013\014" ) !==
false ) {
140 $this->
error(
"Cookie names cannot contain any of the following " .
141 "'=,; \\t\\r\\n\\013\\014'" );
145 if ( !$urlEncode && $value !==
null
146 && strpbrk( $value,
",; \t\r\n\013\014" ) !==
false
148 $this->
error(
"Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
152 if (
$path !==
null && strpbrk(
$path,
",; \t\r\n\013\014" ) !==
false ) {
153 $this->
error(
"Cookie paths cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
157 if ( $domain !==
null && strpbrk( $domain,
",; \t\r\n\013\014" ) !==
false ) {
158 $this->
error(
"Cookie domains cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
163 if ( $value ===
null || strlen( $value ) === 0 ) {
164 $dt = gmdate(
"D, d-M-Y H:i:s T", 1 );
165 $buf .=
"Set-Cookie: $name=deleted; expires=$dt; Max-Age=0";
167 $buf .=
"Set-Cookie: $name=";
169 $buf .= rawurlencode( $value );
174 if ( $expires > 0 ) {
175 $dt = gmdate(
"D, d-M-Y H:i:s T", $expires );
176 $p = strrpos( $dt,
'-' );
177 if ( $p ===
false || substr( $dt, $p + 5, 1 ) !==
' ' ) {
178 $this->
error(
"Expiry date cannot have a year greater than 9999" );
182 $buf .=
"; expires=$dt";
184 $diff = $expires - $this->
time();
188 $buf .=
"; Max-Age=$diff";
193 $buf .=
"; path=$path";
195 if ( $domain !==
null && strlen( $domain ) ) {
196 $buf .=
"; domain=$domain";
202 $buf .=
"; HttpOnly";
204 if ( $samesite !==
null && strlen( $samesite ) ) {
205 $buf .=
"; SameSite=$samesite";
212 $this->
error(
"Cannot modify header information - headers already sent" );