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

Revision #3 has been created by Trejder on Feb 26, 2011, 1:49:37 PM with the memo:

Added section about JavaScript
« previous (#2) next (#4) »

Changes

Title unchanged

Cookie management in Yii

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

cookies, cookie

Content changed

[...]
- an day: 86400 seconds,
- a week: 604800 seconds,
- a month (unified = 30 days): 2592000 seconds,
- a year (unified = 12 months of 30 days): 31104000 seconds.

## Cookies in JavaScript
 
 
By default, you can access your cookies from both PHP code (examples above) and JS code incorporated into your pages. Since JS script handling is entirely done in browser, problems with need of reloading page are not related here.
 
 
### httpOnly
 
 
[CHttpCookie.httpOnly](http://www.yiiframework.com/doc/api/1.1/CHttpCookie#httpOnly-detail "") property decides, whether particular cookie is available to both PHP and scripting languages. This is default setting (false). By setting it to true, you will rise your application security (this reduces identity theft through XSS attacks), but your cookies will not be available to JS code.
 
 
### jQuery
 
 
If you're using any widget or other class that is forcing Yii to publish jQuery code, you can make use of jQuery Cookie Plugin (jquery.cookie.js file), that ships with jQuery on-board Yii. Managing cookies this way in JavaScript is extremely easy. For example, to set a cookie, you call:
 
 
 
```php 
$.cookie('the_cookie', 'the_value');
 
```
 
 
To read it - use standard jQuery approach of:
 
 
 
```php 
var cookie = $.cookie('the_cookie');
 
```
 
 
And to delete cookie, simple set its value to NULL
 
 
 
```php 
$.cookie('the_cookie', null);
 
```
 
 
Look for _jquery.cookie.js_ file published in your assets folder and read its head comment for more examples of using cookies with this jQuery plugin.
 
 
### Non-jQuery approach
 
 
If you don't use jQuery in your application or for other reasons don't want to use jQuery Cookie Plugin, you can use these two functions for accessing cookies in JavaScript:
 
 
 
```php 
function readCookie(cookieName)
 
{
 
        var theCookie = '' + document.cookie;
 
        var ind = theCookie.indexOf(cookieName);
 
        var ind1 = theCookie.indexOf(';', ind);
 
        if(ind1 == -1) ind1 = theCookie.length;
 
 
        if(ind == -1 || cookieName == '') return '';
 
 
        return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
 
}
 
 
function setCookie(cookieName, cookieValue, nDays)
 
{
 
        var today = new Date();
 
        var expire = new Date();
 
 
        if(nDays == null || nDays == 0) nDays = 1;
 
 
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
 
 
        document.cookie = cookieName + '=' + escape(cookieValue) + ';expires=' + expire.toGMTString();
 
}
 
```
 
 
As you can see (second function) - time offset in JavaScript is counted in **miliseconds**, therefore you must recalculate above mentioned example time periods times 1000.
 
 
## Further reading

- [CHttpCookie](http://www.yiiframework.com/doc/api/1.1/CHttpCookie/ "") class reference,
- [CCookieCollection](http://www.yiiframework.com/doc/api/1.1/CCookieCollection/ "") class reference,
- [Handling Cookies](http://www.yiiframework.com/forum/index.php?/topic/15820-handling-cookies "") forum thread,
[...]
38 1
40 followers
Viewed: 193 347 times
Version: 1.1
Category: How-tos
Written by: Trejder
Last updated by: resurtm
Created on: Feb 26, 2011
Last updated: 11 years ago
Update Article

Revisions

View all history