“Cookie law” tutorial – Banner


Difficulty

In this episode I will help you avoid problems with the notorious law, so called, “Cookie Law” (or law eats biscuits, for friends). This law obliges any website owner to insert a banner that signals the presence of cookies on their site, if tracer, even if coming from third parties.

To implement the banner you need a pinch of php code and the html for the banner itself.

Let’s start by creating the actual banner that will be displayed at the bottom or above our website, inserting it in a php script that we will call cookie.php:

<div id="cookielaw_div">
  <span id="cookielaw_icon"></span>
  Banner for cookies on this web site... 
  <a href="privacy_policy.html" target="_blank">Cookie Policy</a>
  <a id="cookielaw_exit" href="cookie/cookie.php?cookie_accept"></a>
</div>
a,a:link,a:visited{
  text-decoration:none;color:#EFEFEF;
}
#cookielaw_div {
  z-index:1000;
  position:fixed;
  width:auto;
  bottom:0;
  left:0;
  right:0;
  height:auto;
  background-color:#292929;
  padding:8px 50px 8px 25px;
  color:#969696;
  font-family:arial;
  font-size:12px;
  min-height:16px;
}
#cookielaw_icon {
  background:url('cookieLaw.png') no-repeat;
  background-position:0 0;
  float:left;
  width:15px;
  height:15px;
  margin:0 15px 0 0;
}
#cookielaw_exit {
  color:#FFFFFF;
  background:url('cookieLaw.png') no-repeat;
  background-position:-15px 0;
  float:right;
  width:15px;
  height:15px;
}

We create the cookie that will maintain the site’s assent to the use of cookies. Important of the cookie is only the key and not the value, which in this case we set to true as an example:

<?php
  if(isset($_GET['cookie_accept'])) {
    setcookie('cookie_accept','true', time()+9000000,'/', 'www.tuosito.it');
    if(!empty($_SERVER['HTTP_REFERER'])) {
      header("Location: ".$_SERVER['HTTP_REFERER']);
    } else {
      header("Location: http://blog.idrobyte.it/");
    }
    exit;
  }
?>

We wrap our banner with an if that prevents the banner from being displayed if the user gives the ok to the cookies on the site:

<?php if (!isset($_COOKIE['cookie_accept'])): ?>
<div id="cookielaw_div">...</div>
<?php endif; ?>

Finally, there is nothing left to do but insert our mini library in all the footers of the site with the good old require:

<?php require_once 'cookie/cookie.php'; ?>

And that’s it.
I hope this tutorial has been helpful.
See you next time.

0
Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.