Using Cookies with CMS Made Simple

CMS Made Simple Beyond the Basics – Using Cookies

So, you need to set a cookie in your CMS Made Simple website. Cookies are convenient ways to store and access information to help make a site more interactive or enhance your user’s experience. Cookies can be set and read using PHP, Smarty tags, and Javascript. As an owner of a CMS Made Simple, you have a full array of options available to use cookies.

Cookie_jar from

~Using Javascript to Set a Cookie~
Javascript can be used directly in your Layout Template to set a cookie. A small amount of Javascript is needed to build a create a cookie function. The below function is a minified version of createCookie() function detailed in this classic Quirks Mode article.

function createCookie(e,t,i){if(i){var o=new Date;o.setTime(o.getTime()+1e3*60*60*24*i);var a="; expires="+o.toGMTString()}else var a="";document.cookie=e+"="+t+a+"; path=/"}

That can be set as inline Javascript in your template – be sure to wrap the script inside of {literal}{/literal} so the Smarty Template Engine will not try to parse the Javascript as Smarty tags. Some developers use an external Javascript file to hold all needed libraries and link it inside of their template.

Next, inside of your template or Javascript file, use the following to set a cookie:


The “7” is the number of days before the cookie will expire. Use “0” to make it a session cookie that will expire at the end of the user’s session.

The “cookieValue” can be a text value or a value held inside of a Smarty var. For example, if you wanted to set a cookie with the username of a logged in front end user, you could do something like:


Then cookie can be accessed and made accessible for Javascript or PHP/Smarty scripts.

Reading and accessing cookies in your Layout Templates is rather easy. But, before we explore how to do that, let’s take a look at how we can set cookies using a User Defined Tag.

~Using PHP in a UDT to Set a Cookie~
We can use a small User Defined Tag (UDT) to set a cookie in any template or page. The User Defined tag can be set with parameters to determine the cookie’s name, value, and how many days before expiration. Under the Extensions menu in the Admin Console, Go to User Defined Tags. Then create a new one called, “setCookie”.

Enter the below PHP and submit. The UDT requires the following parameters: name, value, and days.

if (!isset($params['days'])) { 
	print('<p><b>Error</b>: the setCookies parameter "days" is required</p>');
} elseif ( $params['days'] == '' ) {
	$expire = '0';
} else {
	$expire = time() + ($params['days']*86400);
if (!isset($params['name'])) { 
	print('<p><b>Error</b>: the setCookies parameter "name" is required</p>');
} else {
	$name = $params['name'];
if (!isset($params['value'])) {
	print('<p><b>Error</b>: the setCookies parameter "value" is required</p>');
} else {
	$value = $params['value'];
if (isset($params)) {

Inside of your page or template, create a cookie using the following tag:
{setCookie name=’MyCookie’ value=’MyValue’ days=’7′}

And, of course, any of the parameters can be set dynamically using Smarty vars:
{setCookie name=’MyCookie’ value=$MyValue days=’7′}

To create a Session only cookie, enter an empty value for the days parameter: days=”
To erase the cookie, enter a negative value for the days parameter: days=’-1′

~Reading Cookies~
Now that a cookie has been set, it needs to be read if it will be used for anything useful in a template. Fortunately, Smarty makes that incredibly easy. Just use {$smarty.cookies.MyCookie} where, “MyCookie” is the name of the cookie that should be read. Then, the cookie value can be used in some if/then logic to enhance the user experience or modify the functionality of a page. For example, if a cookie called, “username” was set with the username of a logged in front end user, it can be used to retrieve the person’s username the next time that user returns to the site. When the user returns, something like this can be used to greet the person by username or return a default greeting.

{if isset($smarty.cookies.username)}Hello {$smarty.cookies.username}!{else}Hello Guest!{/if}

Then, you can check if the user is logged in and if they are not, provide a login form.

{if !$ccuser->loggedin()}Please log in to your account…{/if}

I hope this cookies crash course helps you set and read cookies inside of your CMS Made Simple website! Please post comments, suggestions, and questions below.

Looking for quality CMS Made Simple Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under CMS Made Simple | RSS 2.0

Author Spotlight


I've been a full-time front-end web developer for nine years working with design and marketing agencies and businesses located all over the USA. CMS Made Simple has been my go-to Content Management System since mid 2006.

Leave a Reply

Your email address will not be published. Required fields are marked *