Making a Cookie Jar
By Randal L. Schwartz
Ahh, cookies. Not one of my favorite subjects. Cookies, I've decided, are generally mostly evil. Every site I've visited seems to want to send me cookies (as Internet Explorer is happy to report). For the most part, they want to do things like target advertising at me, or even see how often I've been to their particular site.
And more and more, cookies are being used to identify me as a particular person to trigger access to customized features (like my.yahoo.com or my online banking service). And that's annoying. Why? Because I use a half-dozen browsers (if you count agents made from Perl scripts) in a day, and cookies are associated with a particular browser, not a particular person.
So, what to do? I need to have a cookie jar that holds all my cookies. I thought a bit about it, and came up with an interesting approach. What if I were to direct all my Web requests through a proxy server, which could extract cookies from the responses, and add the right cookies to future requests? Aha! That would solve it.
Now, I've already written a Perl proxy server (which I discussed in this column in the February 1997 issue), and I could have wired my cookie jar into that server and kept it running. But some of the browsers I use are located at client sites where I'm teaching or consulting, and it's not always possible to change their proxy settings in a way that would work.
So I took a different approach: a "poor man's proxy," handled entirely as a CGI script! I'd simply invoke the script, extending the URL of the script to include the URL of the page I wanted to fetch, and the program would fetch the page, handling the incoming and outgoing cookies.