emit

Learn how to write the session cookies to the response.

This method writes the session cookies, such as the client ID and user token, as Set-Cookie headers.

Call it before sending any output. Use getResponseCookies instead when integrating with a framework that manages the response.

Signature

This method has the following signature:

public function emit(?callable $emitter = null): void

Example

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

<?phpuse Croct\Plug\CookieStorage;
$storage = CookieStorage::fromGlobals();$storage->emit();

Parameters

The following list describes the supported parameters:

emitter(optional)
callable

The function used to send each cookie. It returns whether the cookie was sent.

The function receives the following arguments:

name
string

The name of the cookie.

value
string

The value of the cookie.

options
array

The cookie options.

expires
int

The expiration time as a Unix timestamp in seconds, or 0 for a session cookie.

path
string

The path on the server where the cookie is available.

domain
string

The domain the cookie is available to.

secure
boolean

Whether the cookie is sent only over HTTPS.

httponly
boolean

Whether the cookie is inaccessible to JavaScript.

samesite(optional)
string

The SameSite policy, one of None, Lax, or Strict.

Default:setcookie