49 $supportsAssoc = version_compare( PHP_VERSION,
'7.3.0',
'>=' );
50 if ( $supportsAssoc ) {
53 return setcookie( $name, $value, $options );
61 if ( !isset( $options[
'samesite'] ) || !strlen( $options[
'samesite'] ) ) {
102 $func = $urlEncode ?
'setcookie()' :
'setrawcookie()';
110 foreach ( $options as $key => $opt ) {
111 if ( $key ===
'expires' ) {
112 $expires = (int)$opt;
114 } elseif ( $key ===
'path' ) {
115 $path = (string)$opt;
117 } elseif ( $key ===
'domain' ) {
118 $domain = (string)$opt;
120 } elseif ( $key ===
'secure' ) {
121 $secure = (bool)$opt;
123 } elseif ( $key ===
'httponly' ) {
124 $httponly = (bool)$opt;
126 } elseif ( $key ===
'samesite' ) {
127 $samesite = (string)$opt;
130 $this->
error(
"$func: Unrecognized key '$key' found in the options array" );
134 if ( $found == 0 && count( $options ) > 0 ) {
135 $this->
error(
"$func: No valid options were found in the given array" );
138 if ( !strlen( $name ) ) {
139 $this->
error(
'Cookie names must not be empty' );
141 } elseif ( strpbrk( $name,
"=,; \t\r\n\013\014" ) !==
false ) {
142 $this->
error(
"Cookie names cannot contain any of the following " .
143 "'=,; \\t\\r\\n\\013\\014'" );
147 if ( !$urlEncode && $value !==
null
148 && strpbrk( $value,
",; \t\r\n\013\014" ) !==
false
150 $this->
error(
"Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
154 if (
$path !==
null && strpbrk(
$path,
",; \t\r\n\013\014" ) !==
false ) {
155 $this->
error(
"Cookie paths cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
159 if ( $domain !==
null && strpbrk( $domain,
",; \t\r\n\013\014" ) !==
false ) {
160 $this->
error(
"Cookie domains cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
165 if ( $value ===
null || strlen( $value ) === 0 ) {
166 $dt = gmdate(
"D, d-M-Y H:i:s T", 1 );
167 $buf .=
"Set-Cookie: $name=deleted; expires=$dt; Max-Age=0";
169 $buf .=
"Set-Cookie: $name=";
171 $buf .= urlencode( $value );
176 if ( $expires > 0 ) {
177 $dt = gmdate(
"D, d-M-Y H:i:s T", $expires );
178 $p = strrpos( $dt,
'-' );
179 if ( $p ===
false || substr( $dt, $p + 5, 1 ) !==
' ' ) {
180 $this->
error(
"Expiry date cannot have a year greater than 9999" );
184 $buf .=
"; expires=$dt";
186 $diff = $expires - $this->
time();
190 $buf .=
"; Max-Age=$diff";
195 $buf .=
"; path=$path";
197 if ( $domain !==
null && strlen( $domain ) ) {
198 $buf .=
"; domain=$domain";
204 $buf .=
"; HttpOnly";
206 if ( $samesite !==
null && strlen( $samesite ) ) {
207 $buf .=
"; SameSite=$samesite";
214 $this->
error(
"Cannot modify header information - headers already sent" );