session_set_cookie_params()

session_set_cookie_params() — Set the session cookie parameters.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Set cookie parameters defined in the php.ini file. The effect of this function only lasts for the duration of the script. Thus, you need to call session_set_cookie_params() for every request and before session_start() is called.

Signature

session_set_cookie_params(int $lifetime_or_options, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null): bool session_set_cookie_params(array $lifetime_or_options): bool

Parameters

ParameterTypeWhat it takes
$lifetime_or_optionsintWhen using the first signature, lifetime of the session cookie, defined in seconds.
$path?stringPath on the domain where the cookie will work. Use a single slash ('/') for all paths on the domain.
$domain?stringCookie domain, for example 'www.php.net'. To make cookies visible on all subdomains then the domain must be prefixed with a dot like '.php.net'.
$secure?boolIf true cookie will only be sent over secure connections.
$httponly?boolIf set to true then PHP will attempt to send the httponly flag when setting the session cookie.

Return value

Returns true on success or false on failure.

Example

The PHP manual ships no example for session_set_cookie_params(); the signature and the parameter table above are the contract.

Changelog

VersionChange
8.5.0The "partitioned" entry was added in the lifetime_or_options array. If set to true, the cookie will be marked as Partitioned (CHIPS).
8.0.0path, domain, secure and httponly are nullable now.
7.3.0An alternative signature supporting an lifetime_or_options array has been added. This signature supports also setting of the SameSite cookie attribute.
7.2.0Returns true on success or false on failure. Formerly the function returned void.

See also

session_get_cookie_params()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page session_set_cookie_params() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.