Forum Moderators: DixonJones

Message Too Old, No Replies

Cookies

How Do I Create A Good Cookie?

         

Jackal

7:31 am on Feb 14, 2005 (gmt 0)



Hello Everyone!

I am new to this so please be patient. I did some searching but was unable to find information relating to my questions. So, here goes!

1. How do I write a good cookie and have the information I want tracked and reported to me? I use a paid traffic analysis tool from my hosting provider and was wanting to do additional things that I could deploy on my websites that would give me additional information. With respect to creating and writing a good cookie, what folder and what file should I create to store this information or is it safer to have this information sent to me and in what manner?

2. How do I create a popup window that recognizes the cookie and greets the customer when they return? Obviously, there are those who can delete cookies when they return to my websites so I would like to also write the cookies to prompt them to enable cookies in their browser if they are disabled and also to register with my websites if they have never done so before so I can do all of this and track all of this? Thank you in advance! Any information, websites, and resources would be greatly appreciated! :)

Jackal

dcrombie

3:53 pm on Feb 14, 2005 (gmt 0)



1) Cookies are only stored in the browser and not on the website/web server. You won't have access to cookie information unless you use a server-side script to capture/send the information.

2) Google for "JavaScript cookies" and you'll find some good resources.

rocknbil

6:52 am on Feb 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah it's a bit of a complicated question, but some basics:

There are two cookies, a session cookie and a persistent cookie, originally devised to maintain a login for the WWW logins you see.

The session cookie is easy. From your server side program, before printing anything else, you print

print "set-cookie:my_cookie_name=my_cookie_value;\n";

The SINGLE NEWLINE is important.

This cookie can be read from the environment variables sent to the server by the browser (perl example:)

if ($ENV{HTTP_COOKIE}) {
$ENV{'HTTP_COOKIE'} =~ s/\s+//g; ## Remove spaces
(@cookiegroups) = split(/;/,$ENV{'HTTP_COOKIE'}); #There may be more than one, see persistents
foreach $grp (@cookiegroups) {
($cookkey,$cookval) = split (/=/,$grp);
if ($cookkey eq 'my_cookie_name'){
$my_cookie_value = $cookval;
}
}
}

When the browser closes, a session cookie ceases to exist. Poof. It's gone. This is a good thing to protect visitor privacy, but if you want to do something when the visitor returns, you need to set a persistent cookie, one that writes to the cookie-list and stays until the user manually deletes it or they visit again.

The persistent cookie is the best tool given to a programmer, but most abused by spammers and marketers. This one is actually WRITTEN to the user's computer; the location varies based on the browser. A persistent cookie is set by using a key/value, path info, and a VERY EXPLICIT expire time in a finiky GMT format:

print "set-cookie:my_cookie_name=my_cookie_value; path=/;expires:Sun 01-Feb-2003 00:00:00 GMT\n";

However: what you ask is a bit difficult. Since cookies can only be read on the exact server from which they were set (in theory,) to return a report of cookies set on your server, you have to have programming to read the data.

Which is my (uninvestigated) guess as to how all those spammers collect user data when they're not even on the site. They offer a free program to do one function, but part of it's functioning is to collect server data and pass it on to home base.

To do your pop-up? Easy! First visit in, you set the cookie

print "set-cookie:my_visitor_id=1234; path=/;expires:Sun 01-Feb-2525 00:00:00 GMT\n";

Then next time they visit, IF there's an $ENV{HTTP_COOKIE} (there is,) you pick it apart as above and come back with '1234' stored in $my_cookie_value. Then you go select first name and last name from your database where the record ID is 1234.

While this falls under the "stupid web trick" umbrella, it's still a very good exercise for getting cookies to work for you and more importantly for your visitors - when so many cookies are working against them.