Category Archives: Cookies

Learning About Cookies – Not the Delicious Kind, but it Still Made Me Feel Great!

Cookies for Geeks

Great idea for corporate gifts from a Russian tech agency! See story on Behance

You know those times when you do something people didn’t think you could do? Those times are pretty much week-makers for me.

Today, a client had a problem with language selection and persistent cookies (where return visitors to a site their language settings are saved from any previous visit).

Someone (not naming names) was asking everyone in the office for help…except me.

Infuriated, I piped up and said that as I am doing all this coding study – I would be happy to look into it. (I am pretty sure they expected me to come back empty-handed.)

The first thing I did was to have a look at all the cookies they currently had on their site. This is where I noticed the first big red flag – apart from cookies from other sites (Google, social sharing, etc), their cookies were all single session.

We needed to change their cookies to include an expiration date, so that their cookies would become persistent cookies.

The second thing we needed to do was to set a variable on each page to show what language that page is, and include it into the cookie. This is done in PHP – which I haven’t really learnt a lot about, but you know what? I was happy to discover that my Javascript training helped me understand the PHP.

Finally, once you have the persistent cookie with the language variable in place, you need to have code on each page so that when a user returns to the site you can get the information out of the cookie and deliver them the appropriate web page. (The phrasing for cookies is usually setting cookies and getting cookies).

After I figured out the issue, I went and explained it to the account manager. Due to inexplicable self esteem issues – I couched my advice in “I think”, and “maybe” and at the end rounded it off with “is that right do you think?”

Actually, I should have been all “Hey, check it out motherfcker! This is how you do it!”.

Because I was right. And they didn’t think I could do it.

Interesting things I learnt while figuring out this problem:

  • You need to set cookies prior to the <html> tag, because you need to do it before any output is sent to the browser (e.g. so in this example, before they are sent any default language page)
  • When investigating the similarities between PHP and JavaScript I learnt this:

“Programs written in JavaScript run in the web browser itself, so if your website has a JavaScript program, the program will be automatically fetched by your visitor’s browser and executed on his/her computer. PHP, on the other hand, runs on the computer where your website is located, that is, on your web host’s computer. After the PHP program does what it needs to do, it sends the result to the visitor’s web browser, which merely displays the results.”

  • I already knew the following, but it is important to remember when changing any cookies, especially on EU sites: You must update the information on your site about the cookies you collect (e.g. on your privacy page).
  • As with any forum, you can’t believe everything you read on StackOverflow, but you can piece things together from all the very helpful community members there. It is an AMAZING resource, although it intimidates me with all the things I don’t know yet. (p.s. you can’t believe everything you read on my site either, obviously).

Other things going on in my learning coding brain

  • Since I am using the codecademy interface so much, in normal email/Word/whatever, whenever I type a “, ( or { – I keep expecting the closing one to be automatically inserted.
  • I am on day 31 of my Codecademy streak


Leave a reply