HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

session_set_cookie_params

Description

The session_set_cookie_params of Session for PHP sets the session cookie parameters.

Syntax

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

Parameters

options

An associative array which may have any of the keys lifetime, path, domain, secure, httponly and samesite. The values have the same meaning as described for the parameters with the same name. The value of the samesite element should be either Lax or Strict. If any of the allowed options are not given, their default values are the same as the default values of the explicit parameters. If the samesite element is omitted, no SameSite cookie attribute is set.

lifetime

The lifetime of the session cookie, defined in seconds.

path

Path on the domain where the cookie will work. Use a single slash ('/') for all paths on the domain.

domain

Cookie 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

If true cookie will only be sent over secure connections.

httponly

If set to true then PHP will attempt to send the httponly flag when setting the session cookie.

Return

Returns true on success or false on failure.

Examples

1 · options

<?

$options =
[
    "lifetime" => 60,
    "path" => "/mypath/",
    "domain" => ".mydomain.com",
    "secure" => true,
    "httponly" => true,
    "samesite" => "strict"
];

$return = session_set_cookie_params($options);

var_export($return);

$session_get_cookie_params = session_get_cookie_params();

echo PHP_EOL;
print_r($session_get_cookie_params);
true
Array
(
    [lifetime] => 60
    [path] => /mypath/
    [domain] => .mydomain.com
    [secure] => 1
    [httponly] => 1
    [samesite] => strict
)

2 · lifetime

<?

$lifetime = 60;

$return = session_set_cookie_params($lifetime);

var_export($return);

$session_get_cookie_params = session_get_cookie_params();

echo PHP_EOL;
print_r($session_get_cookie_params);
true
Array
(
    [lifetime] => 60
    [path] => /
    [domain] => 
    [secure] => 
    [httponly] => 
    [samesite] => 
)

3 · path

<?

$lifetime = 60;
$path = "/mypath/";

$return = session_set_cookie_params($lifetime, $path);

var_export($return);

$session_get_cookie_params = session_get_cookie_params();

echo PHP_EOL;
print_r($session_get_cookie_params);
true
Array
(
    [lifetime] => 60
    [path] => /mypath/
    [domain] => 
    [secure] => 
    [httponly] => 
    [samesite] => 
)

4 · domain

<?

$lifetime = 60;
$path = "/mypath/";
$domain = ".mydomain.com";

$return = session_set_cookie_params($lifetime, $path, $domain);

var_export($return);

$session_get_cookie_params = session_get_cookie_params();

echo PHP_EOL;
print_r($session_get_cookie_params);
true
Array
(
    [lifetime] => 60
    [path] => /mypath/
    [domain] => .mydomain.com
    [secure] => 
    [httponly] => 
    [samesite] => 
)

5 · secure

<?

$lifetime = 60;
$path = "/mypath/";
$domain = ".mydomain.com";
$secure = true;

$return = session_set_cookie_params($lifetime, $path, $domain, $secure);

var_export($return);

$session_get_cookie_params = session_get_cookie_params();

echo PHP_EOL;
print_r($session_get_cookie_params);
true
Array
(
    [lifetime] => 60
    [path] => /mypath/
    [domain] => .mydomain.com
    [secure] => 1
    [httponly] => 
    [samesite] => 
)

6 · httponly

<?

$lifetime = 60;
$path = "/mypath/";
$domain = ".mydomain.com";
$secure = true;
$httponly = true;

$return = session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);

var_export($return);

$session_get_cookie_params = session_get_cookie_params();

echo PHP_EOL;
print_r($session_get_cookie_params);
true
Array
(
    [lifetime] => 60
    [path] => /mypath/
    [domain] => .mydomain.com
    [secure] => 1
    [httponly] => 1
    [samesite] => 
)