{"id":139,"date":"2011-11-06T21:20:39","date_gmt":"2011-11-07T02:20:39","guid":{"rendered":"http:\/\/pavelshub.com\/blog\/?p=139"},"modified":"2011-11-06T21:20:39","modified_gmt":"2011-11-07T02:20:39","slug":"using-cookies-in-lithium-php","status":"publish","type":"post","link":"https:\/\/pavelshub.com\/blog\/2011\/11\/using-cookies-in-lithium-php\/","title":{"rendered":"Using cookies in Lithium PHP"},"content":{"rendered":"<p><a title=\"RDO Calendar\" href=\"http:\/\/rdocalendar.com\" target=\"_blank\">RDO Calender<\/a> required me to use cookies to keep track of simple session information. For a while I couldn&#8217;t figure it out but after reading the unit tests I got it.<\/p>\n<h3>Configuration<\/h3>\n<p>Instead of using the $_COOKIE superglobal you can use Lithium&#8217;s <a title=\"Lithium Session Adapter\" href=\"http:\/\/lithify.me\/docs\/lithium\/storage\/Session\" target=\"_blank\">Session adapter<\/a> to create a cookie adapter then use inside your controllers. First start off by setting up the adapter in the configuration file.<br \/>\napp\/config\/bootstrap\/session.php (make sure the session.php file is uncommented in app\/config\/bootstrap\/bootstrap.php)<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nSession::config(array(\r\n\t'cookie' =&gt; array('adapter' =&gt; 'Cookie', 'expire' =&gt; '+365 day'),\r\n\t'default' =&gt; array('adapter' =&gt; 'Php')\r\n));\r\n<\/pre>\n<p>Below are all the options, taken from <a title=\"Lithium Cookie Adapter\" href=\"http:\/\/lithify.me\/docs\/lithium\/storage\/session\/adapter\/Cookie\" target=\"_blank\">libraries\/lithium\/storage\/session\/adapter\/Cookie.php<\/a><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t\/**\r\n\t * Default settings for this session adapter.\r\n\t *\r\n\t * @var array Keys are in direct correspondence with the parameters in the PHP-native\r\n\t *      `setcookie()` method. The only difference is that the `expire` value is a\r\n\t *\t\tstrtotime-compatible string instead of an epochal timestamp.\r\n\t *\/\r\n\tprotected $_defaults = array(\r\n\t\t'expire' =&gt; '+2 days', 'path' =&gt; '\/', 'name' =&gt; null,\r\n\t\t'domain' =&gt; '', 'secure' =&gt; false, 'httponly' =&gt; false\r\n\t);\r\n<\/pre>\n<h3>Read\/Write<\/h3>\n<p>After the setup is done, you can read and write cookies using the Session::read and Session:write functions respectively.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nSession::read($key = null, array $options = array());\r\nSession::write($key, $value = null, array $options = array());\r\n<\/pre>\n<p>It&#8217;s fairly straightforward. In options you&#8217;ll have to specify the cookie that you created. Full Session::read example below.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nSession::read('cal.dow', array('name' =&gt; 'cookie'));\r\n<\/pre>\n<p>where &#8216;cookie&#8217; is the name of the adapter we created in the config.<\/p>\n<p>Lithium PHP uses the &#8216;.&#8217; character to give namespaces to the cookie keys. Each key is broken into sections and restructured using &#8216;[&#8216; and &#8216;]&#8217;. The call above will read the cookie of key &#8220;appcookie[cal][dow]&#8221;<\/p>\n<h3>JQuery cookie plugin<\/h3>\n<p>Because I was using the <a title=\"jQuery Cookie Plugin\" href=\"http:\/\/plugins.jquery.com\/project\/Cookie\" target=\"_blank\">jquery.cookie.js<\/a> plugin in conjunction; I want to point out that the plugin (and possibly others) use encodeURIComponent() to escape keys\/values when setting cookies. This escaped the &#8216;[]&#8217; characters used by lithium. I modified the aforementioned plugin to accept a rawKey parameter that will prevent the key from being escaped.<br \/>\nrelevant excerpt:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nreturn (document.cookie = &#x5B;\r\n            options.rawKey? key : encodeURIComponent(key), '=',\r\n            options.raw ? value : encodeURIComponent(value),\r\n            options.expires ? '; expires=' + options.expires.toUTCString() : '', \/\/ use expires attribute, max-age is not supported by IE\r\n            options.path ? '; path=' + options.path : '',\r\n            options.domain ? '; domain=' + options.domain : '',\r\n            options.secure ? '; secure' : ''\r\n        ].join(''));\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>RDO Calender required me to use cookies to keep track of simple session information. For a while I couldn&#8217;t figure it out but after reading the unit tests I got it. Configuration Instead of using the $_COOKIE superglobal you can &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/pavelshub.com\/blog\/2011\/11\/using-cookies-in-lithium-php\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[39,41,38,26,40],"class_list":["post-139","post","type-post","status-publish","format-standard","hentry","category-projects","tag-cookies","tag-jquery","tag-lithium-php","tag-php","tag-session"],"_links":{"self":[{"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/posts\/139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":16,"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":156,"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/posts\/139\/revisions\/156"}],"wp:attachment":[{"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pavelshub.com\/blog\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}