Difference between #3 and #14 of
Cookie management in Yii

Changes

Title unchanged

Cookie management in Yii

Category unchanged

How-tos

Yii version unchanged

Tags changed

cookies, cookie, chttpcookie, ccookiecollection, safe reading

Content changed

[...]
```php
$cookie = Yii::app()->request->cookies['cookie_name']->value;
```

S
ee also "Safe reading" in the next chapter.
 
 
### Writing a cookie
 
 
To write a cookie value, use following code:
 
 
 
```php 
Yii::app()->request->cookies['cookie_name'] = new CHttpCookie('cookie_name', $value);
 
```
 
 
Notice, that cookie name must be given twice! See also "Reload required" in the next chapter.
 
 
### Deleting a cookie
 
 
To delete a cookie, use following code:
 
 
 
```php 
unset(Yii::app()->request->cookies['cookie_name']);
 
```
 
 
or you can delete ALL cookies with:
 
 
 
```php 
Yii::app()->request->cookies->clear();
 
```
 
 
Notice, that cookie will not be deleted until next page reload - see "Reload required" and "Cookie expiration problem" in the next chapter.
 
 
## What to remember about?
 
 
### Cookies are objects
 
 
Cookies in Yii are read from [CHttpCookie](http://www.yiiframework.com/doc/api/1.1/CHttpCookie/ "") which is **an object**, that's why you have add **->value** to your code (see examples in previous chapter). If you omit that, you'll get warning or error saying that object of class CHttpCookie cannot be converted to a string.
 
 
### Safe reading
 
 
On the same basis, as above, if a particular cookie does not exists, a corresponding object (CHttpCookie instance) for it will not be created! Trying to read such cookie will result in error "Trying to get property of non-object". It avoid that, it is always better to read a cookie using ternary operator, like that:
 
 
 
```php 
$cookie = (isset(Yii::app()->request->cookies['cookie_name']->value)) ? Yii::app()->request->cookies['cookie_name']->value : '';
 
```
 
 
This way, you'll get either cookie value or an empty string.
 
 
This example also gives us idea how to check, if particular cookie exits:
 
 
 
```php 
$is_cookie = !empty(Yii::app()->request->cookies['cookie_name']->value
ince Yii 1.1.11 you can also access the cookie value directly without accessing the value property first.
 
 
 
```php 
$value = (string)Yii::app()->request->cookies['cookie_name'];
 
```
 
**Note**: You should only use this if you are sure the cookie exists.
 
Also this may not work on PHP prior to 5.2 (See [PHP.net -> __toString()](http://www.php.net/manual/en/language.oop5.magic.php#object.tostring "PHP.net -> __toString()")).
 
 
See also "Safe reading" in the next chapter.
 
 
### Writing a cookie
 
 
Notice, that you have to specify the name twice, if you want to use this:
 
 
```php 
Yii::app()->request->cookies['cookie_name'] = new CHttpCookie('cookie_name', $value);
 
```
 
See also "Reload required" in the next chapter.
 
 
### Configure a cookie
 
 
Starting with Yii 1.1.11 you can configure an cookie upon creation.
 
 
```php 
$cookieCollection['name'] = new CHttpCookie('name', 'value', $options);
 
```
 
 
The option parameter is internally passed to the public method [CHttpCookie::configure()](http://www.yiiframework.com/doc/api/1.1/CHttpCookie#configure-detail "CHttpCookie::configure()") which accepts an array with the following keys:
 
 
- [domain](http://www.yiiframework.com/doc/api/1.1/CHttpCookie#domain-detail "domain")
 
- [expire](http://www.yiiframework.com/doc/api/1.1/CHttpCookie#expire-detail "expire")
 
- [secure](http://www.yiiframework.com/doc/api/1.1/CHttpCookie#secure-detail "secure")
 
- [path](http://www.yiiframework.com/doc/api/1.1/CHttpCookie#path-detail "path")
 
- [httpOnly](http://www.yiiframework.com/doc/api/1.1/CHttpCookie#httpOnly-detail "httpOnly")
 
 
 
### Deleting a cookie
 
 
To delete a cookie, use following code:
 
 
 
```php 
unset(Yii::app()->request->cookies['cookie_name']);
 
```
 
 
or you can delete ALL cookies with:
 
 
 
```php 
Yii::app()->request->cookies->clear();
 
```
 
 
Notice, that cookie will not be deleted until next page reload - see "Reload required" and "Cookie expiration problem" in the next chapter.
 
 
## What to remember about?
 
 
### Cookies are objects
 
 
Cookies in Yii are read from [CHttpCookie](http://www.yiiframework.com/doc/api/1.1/CHttpCookie/ "") which is **an object**. Also it is important to know that
 
 
```php 
Yii::app()->request->cookies;
 
```
 
is an instance of [CCookieCollection](http://http://www.yiiframework.com/doc/api/1.1/CCookieCollection "CCookieCollection") which extends [CMap](http://www.yiiframework.com/doc/api/1.1/CMap "CMap"), therefore it is possible to use simliar to an array.
 
 
 
### Safe reading
 
 
On the same basis, as above, if a particular cookie does not exists, a corresponding object (CHttpCookie instance) for it will not be created! Trying to read such cookie will result in error "Trying to get property of non-object". To avoid that, it is always better to read a cookie using ternary operator, like that:
 
 
 
```php 
$value = isset(Yii::app()->request->cookies['cookie_name']) ? Yii::app()->request->cookies['cookie_name']->value : '';
 
// or
 
$value = Yii::app()->request->cookies->contains('cookie_name') ?
 
Yii::app()->request->cookies['cookie_name']->value : '';
 
```
 
 
This way, you'll get either cookie value or an empty string.
 
 
This example also gives us idea how to check, if particular cookie exits:
 
 
 
```php 
$is_cookie = !isset(Yii::app()->request->cookies['cookie_name']
);
```
[...]
- [Handling Cookies](http://www.yiiframework.com/forum/index.php?/topic/15820-handling-cookies "") forum thread,

Please, extend this article, if you find any mistakes or that something is missing here.

 
 
## Links
 
 
[Russian version](http://resurtm.com/working-with-cookies-in-yii)
38 1
40 followers
Viewed: 198 891 times
Version: 1.1
Category: How-tos
Written by: Trejder Trejder
Last updated by: resurtm resurtm
Created on: Feb 26, 2011
Last updated: 12 years ago
Update Article

Revisions

View all history