Yii Framework & jQuery Cookie Plugin

Accessing the jQuery Cookie plugin is easy in Yii Framework.  Here is the code you will need to put in your PHP file to include the right script references:

    $cs = Yii::app()->clientScript;
    $cs->registerCoreScript('jquery');
    $cs->registerCoreScript('cookie');
    $cs->registerScriptFile(CController::createUrl('/js/mainsite.js'), CClientScript::POS_HEAD);

Then, in your JavaScript, you can set cookies with:

$.cookie("cookieName", "cookieValue", {path: "/", expires: "7"});

The path tells the cookies to allow full-site access, and expires will set the expiration to a week from now.

Finally, to read your cookie from JavaScript, use the following:

$.cookie("cookieName");

Pretty nifty, huh?

Share this page:
  • Facebook
  • Twitter
  • Reddit
  • Google Bookmarks
  • email
  • Print
  • RSS
  • Digg
  • StumbleUpon
  • del.icio.us
  • Slashdot
  • Technorati
  • Tumblr

Steven Michaels

Steven Michaels specializes in PHP web applications utilizing MediaWiki, WordPress, Yii Framework and others. He is an open source contributor and leads development for both of Hollow Developers' internal projects, HollowGame and SpeechEase.

Facebook comments:

Leave a Reply