Comments? Email me: tar@durge.org |
Cookie Monster!Mmmm, munchies.Some people love them, some people hate them. They are in reality fairly harmless, but some think they will bring about the downfall of civilization. They are, of course, foxes... Oh, some people think the same things about cookies, too. A cookie is a tiny text file that sits on your computer. The server has asked for it to be stored there so it can either recognise you, or to allow you to personalise some aspects of a website. As mentioned last session, www.empty-domain.com now uses cookies in an incredibly brief, restricted and, to be honest, pointless way. You can login to the site, and have it tell you your name all the time. Which is nice... However, cookies can be used in a far more interesting way. For example, you could use cookies to check where on your site a visitor has been. If he goes to the (for example) sports section, you could learn this, and have your advertising to that user lean towards sporting goods. Of course, I'm not going to show you how to do that, I'm going to show you how to do my login box. :) First, we'll need a way to read in data to put in the cookie, in this case the users name. We will have to use a form to do this - naturally the way of getting data will vary depending on the application. We encountered how hitop handles forms last time, but for a brief recap: form variables can be accessed by @GET NAME="FORM_varname" or ${FORM_varname} where varname is the name of the input from the form. So, now we are in a position to create a login box. We need a simple form, that asks for your name and allows you to submit. The following should do the trick. What it produces is shown with a grey background.
The most important things to note from the for are the
Time to start baking...So, we know this page will be called by a form. So we know we should expect some form data. I'll assume you've set up your file as you want, with all the templates you want. I'll just be looking at the code you need to set the cookies. Which is...
And that's it. It is that simple. Right. There are other attributes set in the cookie
specification that you can also use. These are Taking them out of the oven Well, now the website visitor has a text file with their name in it
sitting on their computer. Not a lot of use there, is it? We need a way of
getting the data from them. This is achieved with the imaginatively named
Getting the cookie is just as simple as using
Of course, there are other various issues we haven't covered here - the need to check whether a cookie exists, sending the user back to the page they came from, and so on. These items will be covered in a later session about decision making. Next, however, we move onto database integration. |