Further information on the cookie support in PHP can be found under php.net/setcookie.
To create a Cookie the Cookie class from the APF\tools\cookie namespace provides a constructor that allows to specify all parameters necessary. These are:
|$name||Name of the Cookie. This parameter is mandatory to create a Cookie since it is used as a reference name.|
|$expireTime||Time of expiry. This optional parameter defines the life time of the Cookie. Default is 1 day.|
|$domain||Specifies the domain the Cookie is valid for. Default value is the name of the current host.|
|$path||Specifies the path below the domain the Cookie is valid for. Default value is / which means everything below the domain defined for the Cookie instance.|
|$secure||Defines whether the Cookie is only sent along with secure connections (HTTPS) (true) or not (false). Default value is false.|
|$httpOnly||Defines whether the Cookie can be manipulated via Java-Script (false) or not (true). Default is false.|
In order to create a Cookie or read it's value you may use the following code:
$cookie = new Cookie('name_of_the_cookie'); $tenDaysInSeconds = 60 * 60 * 24 * 10; $cookie = new Cookie( 'name_of_the_cookie', time() + $tenDaysInSeconds );
Depending on the use case scenario you can realize further configuration sets given the optional parameters. Example:
$tenDaysInSeconds = 60 * 60 * 24 * 10; $cookie = new Cookie( 'name_of_the_cookie', time() + $tenDaysInSeconds, '.adventure-php-framework.org', '/Seite', true, true );
The secure Cookie defined within the above code box is valid for the adventure-php-framework.org domain and all it's sub-domains but only with a path named /Seite. Further, the Cookie cannot be manipulated via Java Script.
Reading a Cookie can be done with the getValue() method of the Cookie class. You can optionally pass a default value. Example:
$cookie = new Cookie('name_of_the_cookie'); $value = $cookie->getValue();
In case the Cookie is not existent the default value is returned. If you want to change the default value returned to ease implementation please use the optional argument of the getValue() method. In all other cases, the value of the cookie is returned.
To fill a Cookie with a desired value please use the setValue() method:
$cookie = new Cookie('name_of_the_cookie'); $cookie->setValue('wert_des_cookies');
Setting the value, the Cookie is automatically created.
In order to delete a Cookie you are provided the delete() function:
$cookie = new Cookie('name_of_the_cookie'); $cookie->delete();
As of the next request, the Cookie is no longer available. getValue() will then return the default value.