Cookie

Class Cookie encapsulates available PHP functions for reading and writing of Cookies according to RFC-6265. Given the easy-to-use interface you can use Cookies within your application without deeper knowledge of the underlying PHP API.

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:

Parameter Description
$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:

PHP 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:

PHP code
$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:

PHP code
$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.

3. Writing a value

To fill a Cookie with a desired value please use the setValue() method:

PHP code
$cookie = new Cookie('name_of_the_cookie'); $cookie->setValue('wert_des_cookies');

Setting the value, the Cookie is automatically created.

Please note, that the value of a Cookie is always a string. If you intend to save more complex data structures you must take care of de- and encoding yourself. For this reason, you may want to use the serialize() and unserialize() functions.
Further, please note, that the size of Cookies directly impacts the request performance. This is because Cookies are contained within the HTTP request header. Hence, it is strongly recommended to keep the amount of data small. You may want to use a custom data format or compressing functions.

In order to delete a Cookie you are provided the delete() function:

PHP code
$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.

Comments

Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.