Constructor

Learn how to initialize a cookie.

The constructor initializes the cookie with the given name, value, and attributes.

Only the name and value are required, so you can keep the defaults for the remaining attributes when they suit your response.

Signature

The constructor has the following signature:

public function __construct(    string $name,    string $value,    ?int $expiration = null,    string $path = '/',    ?string $domain = null,    bool $secure = true,    bool $httpOnly = false,    ?string $sameSite = 'None',)

Example

Here is a basic example of how to initialize a cookie:

<?phpuse Croct\Plug\Cookie;
$cookie = new Cookie(    name: 'ct_client_id',    value: '7e9d59a9-e4b3-4f8a-b0c1-2d3e4f5a6b7c',);

Parameters

The following list describes the supported parameters:

name
string

The name of the cookie.

value
string

The value of the cookie.

expiration(optional)
integer

The expiration time as a Unix timestamp in seconds. The default is null, which makes it a session cookie.

Default:null
path(optional)
string

The path for the cookie.

Default:/
domain(optional)
string

The domain for the cookie. The default is null, which scopes the cookie to the current host.

Default:null
secure(optional)
boolean

Whether the cookie is sent only over HTTPS.

Default:true
httpOnly(optional)
boolean

Whether the cookie is hidden from client-side scripts.

Default:false
sameSite(optional)
string

The SameSite policy applied to the cookie.

Default:None