note 129988 added to function.setcookie
| From: | steven@sovstack.com | Date: | Mon, 20 Jan 2025 19:55:25 +0000 |
| Subject: | note 129988 added to function.setcookie | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221804@lists.php.net to get a copy of this message | ||
As of PHP 7.3, you can use this syntax:
setcookie( $name, $value, $options)
Be aware that the array in $options is not fully compatible with array key names. In other words,
the order of your array values matters regardless of the array key names.
<?PHP
// Correct (with key names):
setcookie(
'my_cookie',
'my_value',
[
'expires' => time() + 3600,
'path' => '/',
]
);
// Correct (without key names):
setcookie(
'my_cookie',
'my_value',
[
time() + 3600, // expires
'/' // path
]
);
// Incorrect Usage (wrong order as key names are ignored):
setcookie(
'my_cookie',
'my_value',
[
'path' => '/', // WRONG: should be 2nd
'expires' => time() + 3600, // WRONG: should be 1st
]
);
// Here's the correct order of the $options array with default values:
$options = [
'expires' => 0,
'path' => "",
'domain' => "",
'secure' => false,
'httponly' => false
];
setcookie(
'my_cookie',
'my_value',
$options
);
?>
If you do not provide options, the default values will be used.
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 67.147.66.102)
----
Manual Page -- https://php.net/manual/en/function.setcookie.php
Edit -- https://main.php.net/note/edit/129988
Del: integrated -- https://main.php.net/note/delete/129988/integrated
Del: useless -- https://main.php.net/note/delete/129988/useless
Del: bad code -- https://main.php.net/note/delete/129988/bad+code
Del: spam -- https://main.php.net/note/delete/129988/spam
Del: non-english -- https://main.php.net/note/delete/129988/non-english
Del: in docs -- https://main.php.net/note/delete/129988/in+docs
Del: other reasons-- https://main.php.net/note/delete/129988
Reject -- https://main.php.net/note/reject/129988
Search -- https://main.php.net/manage/user-notes.php