Set, read and debug cookies

  Previous article Next article  

Set, read and debug cookies

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.1.4
  Last updated: 31-07-2016
  Comments: 6

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"

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"

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 ( )


A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

Your IP address will be stored on submit. I reserve the right to change or delete your response without notice!
In case of abuse your IP address will be blocked!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  6 Comments

CMS Made Simple 2.2.2 - Tutorials, tips and tricks - CMSMS 2.2.2

Set, read and debug cookies

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 2.1.4
  Last updated: 31-07-2016
  Comments: 6

A2 Webhosting CMS Made Simple