toSetCookieHeader

Learn how to render the cookie as a Set-Cookie header value.

This method renders the cookie as the value of a Set-Cookie response header.

Use it when your framework lets you set raw header values, so you can write the cookie without mapping each attribute yourself.

Signature

This method has the following signature:

public function toSetCookieHeader(?int $now = null): string

This method returns the Set-Cookie header value that represents the cookie.

Example

Here is a basic example of how to use this method:

<?phpuse Croct\Plug\CookieStorage;
$cookies = CookieStorage::fromGlobals()->getResponseCookies();
foreach ($cookies as $cookie) {    header('Set-Cookie: ' . $cookie->toSetCookieHeader(), false);}

Parameters

The following list describes the supported parameters:

now(optional)
integer

The current time as a Unix timestamp in seconds, used to compute the Max-Age attribute. The default is null, which uses the current time.

Default:null