Miscellaneous Web Gizmos

Setting a cookie on an e-mail newsletter

Setting a cookie on a pdf

Setting a Cookie on a PDF

If you run a link to a pdf, or other binary file, in an e-mail message, how can you track the users and set cookies on their browsers for future reference? The answer lies in putting an intermediate step into the process. Link to a cgi script, with the information you want set in the query string. A simple redirect does the rest.

<a href="/root/cgi/n.pl? <userid>&<path-to-file>">click me!</a>

In this instance, I use a meta redirect to the actual file I want, setting the cookie via javascript on the interstitial page. The query string will have the unique user ID for the recipient, and the path to the real file. Since this is a redirect, the destination file is in the web directory somewhere.

my ($min, $hour, $day, $month, $year) = (localtime)[1,2,3,4,5]; my $today = ($year + 1900) . ($month + 1) . $day; my $time = sprintf ("%02d%02d%04d%02d%02d", $month+1, $day, $year+1900, $hour, $min); my ($user, $doc) = split /\&/, $ENV{'QUERY_STRING'}; my $host = $ENV{'HTTP_HOST'} | $ENV{'HOST'}; #user is $user, #document to deliver is $doc print "Content-type:text/html\n\n"; print <<END; <html> <head> <meta http-equiv="Refresh" content="0; URL=http://$host/$doc"> <title>Welcome</title> <script language = "JavaScript"> <!-- hide me var the_name = "$user"; var the_cookie = "username=" + escape(the_name); var the_date = new Date("May 31, 2006"); var the_cookie_date = the_date.toGMTString(); the_cookie = the_cookie + ";expires="; the_cookie += the_cookie_date + ";path=/"; document.cookie = the_cookie; // show me--> </script> </head> <body> <a href="http://$host/$doc">Continue</a> </body> </html> END open FILE, ">>/_private/cookiepeople/$today.txt"; print FILE join "\t", $time, $user, $ENV{'REMOTE_ADDR'}, "$host/$doc", "\n"; close FILE;

Of course, add a link in case the meta refresh doesn't work. After the reader has moved on, we make an tab-delimited entry in a log file for future reference.