{"id":94,"date":"2015-06-01T11:40:42","date_gmt":"2015-06-01T05:40:42","guid":{"rendered":"http:\/\/ronniee.net\/?p=94"},"modified":"2015-06-01T11:40:42","modified_gmt":"2015-06-01T05:40:42","slug":"joomla-cookie-api","status":"publish","type":"post","link":"https:\/\/ronniee.net\/?p=94","title":{"rendered":"Joomla Cookie API"},"content":{"rendered":"\n<pre class=\"lang:php decode:true \" >\/\/ Get input cookie object\r\n$inputCookie  = JFactory::getApplication()-&gt;input-&gt;cookie;\r\n\r\n\/\/ Get cookie data\r\n$value        = $inputCookie-&gt;get($name = 'myCookie', $defaultValue = null);\r\n\r\n\/\/ Check that cookie exists\r\n$cookieExists = ($value === null);\r\n\r\n\/\/ Set cookie data\r\n$inputCookie-&gt;set($name = 'myCookie', $value = '123', $expire = 0);\r\n\r\n\/\/ Remove cookie\r\n$inputCookie-&gt;set('myCookie', null, time() - 1);<\/pre>\n<p><strong>Some rules about $expire value<\/strong><\/p>\n<p>    its a Unix tinestamp in seconds, like return value of time().<br \/>\n    $expire == 0: cookie lifetime is of browser session.<br \/>\n    $expire < time(): cookie is being deleted (expire set to past). You could remove cookie by setting it's value to null, but apparently IE fails to do so.\n\n<strong>Notes<\/strong><\/p>\n<p>Keep in mind that cookies should be set before headers are sent (usually before output is echoed).<\/p>\n<p>Cookie key and value should be properly escaped<\/p>\n<p><strong>Non-string values<\/strong><\/p>\n<p>When serializing the value on set (like json_encode($dataNode)), remember to use proper filter to retrieve it later on. Default is cmd, which filters out pretty much anything but a-Z, 0-9 and cracks JSON structure.<\/p>\n<pre class=\"lang:php decode:true \" >\/\/ Get cookie data\r\n$encodedString = $inputCookie-&gt;get('myCookie', null, $filter = 'string');\r\n\r\n\/\/ Decode\r\n$values = json_decode($encodedString);\r\n\r\n\/\/ Encode and Set\r\n$inputCookie-&gt;set('myCookie', json_encode($values));<\/pre>\n<p>Rererences<\/p>\n<p>    Joomla CMS github repository: <a href=\"https:\/\/github.com\/joomla\/joomla-cms\/blob\/master\/libraries\/joomla\/input\/cookie.php#L47\">JInputCookie::set<\/a> (very well documented)<br \/>\n    php docs: php.net\/setcookie (developer experiences)<br \/>\n    Wikipedia: HTTP Cookies (theory)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ Get input cookie object $inputCookie = JFactory::getApplication()-&gt;input-&gt;cookie; \/\/ Get cookie data $value = $inputCookie-&gt;get($name = &#8216;myCookie&#8217;, $defaultValue = null); \/\/ Check that cookie exists $cookieExists = ($value === null); \/\/ Set cookie data $inputCookie-&gt;set($name = &#8216;myCookie&#8217;, $value = &#8216;123&#8217;, $expire = 0); \/\/ Remove cookie $inputCookie-&gt;set(&#8216;myCookie&#8217;, null, time() &#8211; 1); Some rules about $expire [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,2],"tags":[],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-joomla3x","category-php"],"_links":{"self":[{"href":"https:\/\/ronniee.net\/index.php?rest_route=\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ronniee.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ronniee.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ronniee.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ronniee.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=94"}],"version-history":[{"count":1,"href":"https:\/\/ronniee.net\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/ronniee.net\/index.php?rest_route=\/wp\/v2\/posts\/94\/revisions\/95"}],"wp:attachment":[{"href":"https:\/\/ronniee.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ronniee.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ronniee.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}