Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-03-08 21:27:53

phual
Member
From: UK
Registered: 2008-02-19
Posts: 26
Website

Setting cookies (EU cookie laws)

I recently realised that one of the software components of my Textpattern site uses a cookies, and hence I need to warn users to comply with EU legislation (I will give them the option of accept of bog off – so nothing complicated).

Here is my first attempt at what I’m going to present (all flashy stuff really – it’s basically a box at the bottom of the page):

<!DOCTYPE html>

<html>
  <head>
    <title>Title of the document</title>

    <style>
      #move {
	animation-name: moving;
	animation-duration: 4s;

	-moz-animation-name: moving;
	-moz-animation-duration: 4s;

	-webkit-animation-name: moving;
	-webkit-animation-duration: 4s;

	-o-animation-name: moving;
	-o-animation-duration: 4s;

	background-color: #ffaa00;
	border-color:  #555555;
	border-radius: 5px;
	border-style: solid;
	border-width: thin;
	opacity: 1;
	padding: 5px;

	position: fixed;

	/* Position element in centre of screen */
	left: 50%;
	margin-left: -40%;
	bottom: 0%;

	min-height: 50px;
	width: 80%;
	}

      @keyframes moving {
	0%   {bottom: 100%; opacity: 0;}
	50%  {bottom: 100%; opacity: 0.1;}
	100% {bottom:   0%; opacity: 1;}
	}

      @-moz-keyframes moving /* Firefox */ {
	0%   {bottom: 100%; opacity: 0;}
	50%  {bottom: 100%; opacity: 0.1;}
	100% {bottom:   0%; opacity: 1;}
	}

      @-webkit-keyframes moving /* Safari and Chrome */ {
	0%   {bottom: 100%; opacity: 0;}
	50%  {bottom: 100%; opacity: 0.1;}
	100% {bottom:   0%; opacity: 1;}
	}

      @-o-keyframes moving /* Opera */ {
	0%   {bottom: 100%; opacity: 0;}
	50%  {bottom: 100%; opacity: 0.1;}
	100% {bottom:   0%; opacity: 1;}
	}
    </style>
  </head>

  <body>

  <p>Some text. </p>

  <div id="move">
    This website places cookies on your computer to help make the website work better. 
    Please follow this link to find out how we use them and for
    <a href="/website#cookies">information on how to change your cookie settings</a>.
  </div>

  </body>

</html>

So far, so good. However, I next want to give the user the option to make the box go away and not come back. This (somewhat ironically) requires me to place a cookie on their computer (probably via a tick box / form / whatever in the message box). I could do this using JavaScript, but I’d like to explore the PHP solution as JavaScript can be turned off. I’m a little nervous about how to do this with PHP in the Textpattern world and would appreciate some advice, especially with where to place code elements and consequences of the approach.

With a bit of luck, this thread will also serve as a simple reference solution for others needing to implement the warning.

Thanks

Stuart

Offline

#2 2013-03-08 22:43:21

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Setting cookies (EU cookie laws)

phual wrote:

as JavaScript can be turned off

Just like JavaScript, cookies are a client-side feature, and they can be blocked too. Avoiding one of the two may not fix anything.

Anyways, you could do it in server-side language too by sending the cookie wish in the response. As far as PHP goes, PHP as a programming language works the same anywhere, even in Textpattern, but you can give it bit of a Textpattern like spin, by using the templating language as a bit of a helper. Textpattern also has few library functions for handling net stuff (cookies, URLs). E.g.

<txp:variable name="cookie_warning_visible"><txp:php>
	if (gps('hide_cookie_warning'))
	{
		setcookie('cookie_warning_hidden', 1, strtotime('+1 year'), '/');
		echo 'false';
	}
	else if (cs('cookie_warning_hidden'))
	{
		echo 'false';
	}
	else
	{
		echo 'true';
	}
</txp:php></txp:variable>

You could include something like the above at the top of your page templates. Create a form template, put the code into it and then include it into each page template using the output_form tag.

What the above code does, is that it creates a new variable named cookie_warning_visible that lets you know whether the user has closed the warning or not. This cookie is populated with false if a cookie named cookie_warning_hidden exists, true otherwise. The code also itself creates that cookie when hide_cookie_warning HTTP GET variable is included in the requested location (e.g. http://example.com/some/location?hide_cookie_warning=1@).

After the code you can freely use variable to hide the message or doing other changes to the templates. E.g.

<txp:variable name="cookie_warning_visible" value="true">
	<div id="cookie-monster">
		This website places cookies on your computer to help ma [...]
		<a href="?hide_cookie_warning=1">Hide this warning</a>
	</div>
</txp:variable>

Note that due to code being handled server-side opposed to by JavaScript, the pages can not really send caching headers (e.g. lastmod headers are no go). If the pages are cached by the client, the presentation will not be affected, since the pages come from the user’s cache and not from your server. Client-side technologies on the other hand wouldn’t be affected by client-side caching. This is one of the downsides you will have to face if you want to have dynamically formed pages on the server’s end without using client-side techniques.

If you do want to save your server resources and wish to use caching in some point in the future, you could of course use JavaScript fallback, but then you would be in square one. The server-side variation would still be affected by caching. If your pages are otherwise already highly dynamic, and you can’t or don’t want to cache them, then this won’t be a problem for you.

Offline

#3 2013-03-09 12:18:17

phual
Member
From: UK
Registered: 2008-02-19
Posts: 26
Website

Re: Setting cookies (EU cookie laws)

The first line of your response is a good point. I was thinking very much along the lines of “we’re going to irritate people with the message where they don’t allow JavaScript”. But of course, we’re going to irritate people who don’t use cookies anyway, so I think that they will just have to lump it.

Thanks for the code and explanation. It’s very helpful and makes me realise the shortcomings of the approach. I use cruft-free URLs and passing the variable into the URL will compromise this (if only for the first time someone accepts the cookie). For this reason, I think that I should maybe re-evaluate and go back to using JavaScript to set the cookie.

Thanks again.

Stuart

Offline

#4 2013-03-10 05:16:44

sochicomputerRU
Member
From: Россия
Registered: 2013-01-18
Posts: 61
Website

Re: Setting cookies (EU cookie laws)

(Post deleted because it is offensive. This forum is not meant for expressing political or other personal views. -Els)

Last edited by els (2013-03-10 15:09:56)

Offline

#5 2013-03-10 13:20:11

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,303

Re: Setting cookies (EU cookie laws)

Stick to the point and don’t be offensive, Alexander, that’s an international tradition.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#6 2013-08-20 20:53:53

NicolasGraph
Plugin Author
From: France
Registered: 2008-07-24
Posts: 860
Website

Re: Setting cookies (EU cookie laws)

Thanks to Jukka for the tip!
Actually I’m using txp:if_variable to make it work instead of txp:variable in the second part of the code. Otherwise it’s great.


Nicolas
Follow me on Twitter and GitHub!
Multiple edits are usually to correct my frenglish…

Offline

#7 2013-08-22 00:57:45

tye
Member
From: Pottsville, NSW
Registered: 2005-07-06
Posts: 859
Website

Re: Setting cookies (EU cookie laws)

I was working on a site using CookiCuttr jquery plugin the other day which does something similar I think – not txp solution, but an option

Offline

#8 2015-01-19 21:22:03

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: Setting cookies (EU cookie laws)

I see that for Wordpress (obviously) there are many plugins to compliance the EU cookie Law. For TXP we have an useful tip by Jukka: maybe someone is developing a plug&play plugin with some special effects?

Last edited by candyman (2015-01-19 21:24:18)

Offline

#9 2015-01-19 21:56:04

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Setting cookies (EU cookie laws)

You could not use cookies, problem solved instantly. I’ve removed all cookies from my company’s new website that I’m building right now.

Yes, I’ve dumped Google Analytics too – I’ve never really needed to know stats for a site like that and we don’t pay for Adwords anyway. Most content performance analysis can be done through Google Webmaster Tools and Google Rich Snippets Tool.

That means less JavaScript, leaner site, less snooping, less HTTP requests.

Obviously that doesn’t wash for some client sites, but it’s worth thinking about.

Offline

#10 2015-01-20 00:51:37

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,578
Website

Re: Setting cookies (EU cookie laws)

If you need it nevertheless, there’s also spf_if_eu.


TXP Builders – finely-crafted code, design and txp

Offline

#11 2015-01-20 15:23:12

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: Setting cookies (EU cookie laws)

I was looking for a thing like that, indeed.

Many thanks.

p.s.: @Phil never heard about the Webmaster Tools: I’ll give a look at. Anyway I think it’s safer to have the cookie law snippet anyway…

Last edited by candyman (2015-01-20 15:24:08)

Offline

#12 2015-05-04 19:31:25

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: Setting cookies (EU cookie laws)

This EU Cookie Law is drivin’ me mad.

I was thinking’ to follow Phil’s advice…

philwareham wrote #287535:

You could not use cookies, problem solved instantly. I’ve removed all cookies from my company’s new website that I’m building right now.

… anyway it seems that I can’t. Even TXP has its sessions cookies, hasn’t it?
Someone has the table of TXP cookies created after a fresh install?

Last edited by candyman (2015-05-04 19:32:54)

Offline

Board footer

Powered by FluxBB