set_cookie / delete_cookie
method "set_cookie"
Introduced with LEPTON v5, by calling the set_cookie methode, a cookie is created.
Per default, this created cookie also contains the (unsupported in pre PHP 7.3 versions) samesite attribute; without some browsers might ignore the cookie in the future.
Syntax:
LEPTON_session::set_cookie( $name,
$value,
$options = array(),
$mustExists = false,
$mergeDefault = true );
- $name The name of the cookie
- $value The value of the cookie
- $options (Optional) An array containing none, some or all of the following attributes:
- expires => time() + ( 3 * 3600) // expires in 3 hours
- path => "/"
- domain => ""
- secure => true/false // true for https connections, false otherwise
- httpsonly => true
- samesite => "Lax"
- $mustExists (Optional) A boolean true or false value. When set to true but cookie does not exists, it is not created. Therefore true means update only
- $mergeDefault (Optional) A boolean true or false value. When set to true, the options array is set with the default values, but with overwritten attributes from input options.
Possible return values are:
- true The cookie have been created successfully
- false An error occured during cookie creation. Therefore the cookie has not been created
- null The $mustExists flag has been passed as true but cookie does not exists
For a detailed description of the option attributes and their possible values please check:
- www.php.net/manual/de/function.setcookie.php
- www.php.net/manual/de/session.configuration.php#ini.session.cookie-samesite
Note: for all missing or empty attributes in the options array passed to the set_cookie method,
the defaults listed above are used, except you use as parameter #5 the value false.
To create a cookie, simply do:
LEPTON_session::set_cookie
Create a cookie
- // specify the cookies name
- $name = "myOwnCookie";
- // define a dedicated value
- $value = "";
- // define the attributes differ from the defaults
- $options = array(
- "expires" => time() + (3 * 3600) // expire in 3 hours
- );
- // create a cookie
- LEPTON_session::set_cookie( $name, $value, $options );
method "delete_cookie"
Also introduced with LEPTON v5, by calling the delete_cookie methode, an existing cookie is deleted.
LEPTON_session::delete_cookie
Delete a cookie
- // define the name of the cookie
- $name = "myOwnCookie";
- // delete session cookie and session itself
- LEPTON_session::delete_cookie( $name );