Mar 05, 2016

Minimal Cookie disclosure implementation in SilverStripe

We implement two Controller methods in Page_Controller and explicitly allow the action confirmCookieUsage:

<?php

// Page Controller

class Page_Controller extends ContentController {

  private static $allowed_actions = [
    'confirmCookieUsage' => true,
  ];

  function confirmCookieUsage() {
    if ($this->getRequest()->isPOST()) {
      Cookie::set('CookieUsageConfirmed', true);
    }
  }

  function CookieUsageConfirmed() {
    return Cookie::get('CookieUsageConfirmed');
  }

}
<% if not CookieUsageConfirmed %>
  <div id="desktop-cookie-disclosure" style="position: fixed; z-index: 10000; top: 0; left: 0; width: 100%;">
    <div onclick="$.ajax({ url: 'home/confirmCookieUsage', method: 'post' }); $(this).parent().fadeOut(300, function() { $(this).remove(); });" style="cursor: pointer; color: #fff; background: rgba(0,0,0,0.8); border-radius: 10px; padding: 2px 5px; position: absolute; top: 0.4em; right: 0.5em; font-size: 9px; line-height: 13px;">✖</div>
    <div style="color: #fff; background: #666; padding: 0.5em 1em; font-size: 0.9em; text-align: center;">
      <span style="" lang="en">
        We have placed cookies on your computer to help make this website better. We use a Google Analytics script which sets cookies. More details can be found in our <a href="impressum/#cookies" style="color: #fff;">privacy policy</a>
      </span>
      <span style="display: none;" lang="de">
        Wir verwenden bei Ihrem Besuch auf unserer Webseite Cookies. Indem Sie unsere Webseite benutzen, stimmen Sie unseren Datenschutzrichtlinien zu. <a href="impressum/#cookies" style="color: #fff;">Mehr Informationen dazu</a></span>
      </span>
    </div>
  </div>
<% end_if %>

It's working on all SilverStripe versions 2.x and 3.x