Set, read and debug cookies
Previous article Next articleA cookie, also known as an HTTP cookie, web cookie, or browser cookie, is usually a small piece of data sent from a website and stored in a user's web browser while a user is browsing a website. You can use cookies to store user settings for i.e. website styling, language preferences, etc.
I will show you how to set, read and debug cookies at your CMS Made Simple website.
TIP : Check the following tutorial here at CMS Can Be Simple for a free EU-cookie law solution.
1. Set cookies
With this User Defined Tag (UDT) it is easy to set multiple cookies.
This part of the article originates from the former CMS Made Simple Wiki, the original author is Ken Griffith.
Create a new User Defined Tag named "setCookies"
{
if (isset($params['days']))
{
$expire = time() + ($params['days']*86400);
foreach ($params as $name => $value)
{
if ($name != 'days')
{
setcookie($name, $value, $expire, '/');
unset($value);
}
}
return;
}
else
{
print('<p><b>Error</b>: the setCookies parameter "days" is required</p>');
}
}
else
{
print('<p><b>Error</b>: the setCookies UDT requires parameters...</p>');
return;
}
How to use
You call this UDT in a page like:
This will work with versions of CMSMS that process the body before the head of the document. (Cookies must be sent before the <html>. So if you have it set up that CMSMS processes the head before the body, where your {setCookies} tag is, then it will not work.)
You can have as many cookiename='cookievalue' pairs as you want, with any name you want, as long as each cookie name is unique.
Parameters
- days (required) - This sets the expiration date of the cookie
2. Read cookies
How to use
The simplest way to read the value of a cookie is using Smarty. You can read and use it in a page like:
<p>Cookie #1 is set!</p>
{/if}
<p>Cookie #2 is NOT set!</p>
{/if}
3. Cookie debug
Create a new User Defined Tag named "debugCookies"
How to use
Add in your page or template:
It will give an output of the cookies of the website and the value of them.
Working example
To demonstrate the above really works, I have set up a little demo here.
Set cookies
Using the following line I now have set a testcookie at your browser, it will expire in 1 day.
Read cookies
You might need to refresh your browser when you visit this page for the first time.
<p>TEST Cookie is set!</p>
{else}
<p>TEST Cookie is NOT set!</p>
{/if}
The output is:
TEST Cookie is NOT set!
Debug cookies
You might need to refresh your browser when you visit this page for the first time.
The output is:
Array ( )
Comment Form
ReviewManager
ReviewManager
6 Comments
Fixed notice
{if !empty($smarty.cookies.testcookie) && $smarty.cookies.testcookie == 'testvalue'}
...
{/if}
Ps. You can set your own cookie of course from your website. If logged in set cookie true. If logged out cookie false... Something to play with ;-)
@André
As far as I know the FEU module uses the core session cookie. So doesnt have an own cookie that is "readable" for other purposes...
Hello Rolf,
Great article, thanks for sharing this information.
I wonder if you could help me with a problem.
I need to get a cookie value through .htaccess to allow or deny file access. What I need to know is if the user is logged in or not. Do you know if frontenduser module has this cookie by default (similar to "wordpress_logged_in." in wordpress)? If not, am I able to do this with this method?
Thank you
André.
Thank you for the tip!
I had some problems with the cookies not being usable on some pages, and then I discovered that this problem came from the "path" parameter
The cookies where available on some pages, but not on other. If you encounter that problem, simply change the PHP setcookie function :
setcookie($name, $value, $expire, '/');
Hi Rolf,
Thx for publishing this!
Greetings,
Manuel