MENU

Set, read and debug cookies

  Previous article Next article  

A 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))
{
  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:

{setCookies days='numDays' cookiename='cookievalue' foo='bar'}

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:

{if !empty($smarty.cookies.cookie1) && $smarty.cookies.cookie1 == 'value1'}
   <p>Cookie #1 is set!</p>
{/if}
{if !empty($smarty.cookies.cookie2) && $smarty.cookies.cookie2 != 'value2'}
   <p>Cookie #2 is NOT set!</p>
{/if}

 3. Cookie debug

Create a new User Defined Tag named "debugCookies"

print_r($_COOKIE);

  How to use

Add in your page or template:

{debugCookies}

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.

{setCookies days='1' testcookie='testvalue'}

Read cookies

You might need to refresh your browser when you visit this page for the first time.

{if !empty($smarty.cookies.testcookie) && $smarty.cookies.testcookie == 'testvalue'}
   <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.

{debugCookies}

The output is:
Array ( )


Buy Me A Coffee


  Comment Form

ReviewManager

Click here to open the form

ReviewManager

  6 Comments

Buy Me A Coffee

CMS Made Simple - Tutorials, Tips and Tricks - CMSMS

Set, read and debug cookies

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.2.7
  Last updated: 04-04-2018
  Comments: 6
  http://cms.ms/DF3H


Buy Me A Coffee




Advertisement


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!