Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question:

I've read that CGI.pm just makes using cookies easier and everything, but I would like to know how it is done without using that module.

I'd just like to know how to create, retrieve, and delete cookes. Is it in any way better than using CGI.pm?

Thanks to anybody who replies.

Replies are listed 'Best First'.
Re: Cookies without using CGI.pm
by eduardo (Curate) on Jan 28, 2001 at 06:16 UTC
    Hi Monolith-0. I have a question, and a comment. If you are chosing to find out how it is that you create, retrieve, and delete cookies without using the amazing <a href="http://www.perlmonks.org/index.pl?node=CGI.pm&lastnode_id=3333">CGI.pm</a> for purely academic purposes, then, I applaud you. It is always good to understand the mechanisms at work, especially when they are as common and complex as cookies.

    If you were to do a perldoc perlman:CGI::Cookie you would see that right at the 3rd paragraph it says:

    For full information on cookies see

    http://www.ics.uci.edu/pub/ietf/http/rfc2109.txt.

    This link should give you everything you need about cookies.

    However, here comes the angry old man in me. If you are asking how to do cookies in Perl without using CGI.pm because for some reason you have chosen to roll your own mechanism instead of using the well documented, standard, loved CGI.pm, then please, don't. On this site we have already discussed to death the reasons to use standard modules (a few of the reasons can be found: Yet Another Cargo Cult non-use of CGI.pm, Perfect example of why to use CGI.pm), and other places if you look hard.

    Please, oh please, oh please... use the standard modules in a production setting...
Re: Cookies without using CGI.pm
by Beatnik (Parson) on Jan 28, 2001 at 05:13 UTC
    Well, a way of rolling your own is tweaking the HTTP header and insert the Set-Cookie statement.
    print "Content-type: text/html\n"; print "Set-Cookie: MyCookie=Something\n\n";
    Basically, cookies are part of the HTTP header and have quite a few options... The code above sets a Session cookie (exists as long as the browser stays open). Expiring cookies need an extra expires option etc...
    If you want the fancy stuff, stick to CGI.pm (it's installed by default).

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
      I can understand why you might need this, since some webhosters don't even provide CGI.pm. You can retrieve most cookies with a function like this.
      sub GetCookies { %decode = ('\+'=>' ','\%3A\%3A'=>'::','\%26'=>'&','\%3D'=>'=', '\%2C'=>',','\%3B'=>';','\%2B'=>'+','\%25'=>'%'); my %Cookies = (); foreach (split(/; /,$ENV{'HTTP_COOKIE'})) { my ($cookie,$value) = split(/=/); foreach $ch ('\+','\%3A\%3A','\%26','\%3D','\%2C','\%3B','\%2B','\ +%25') { $cookie =~ s/$ch/$decode{$ch}/g; $value =~ s/$ch/$decode{$ch}/g; } $Cookies{$cookie} = $value; } return %Cookies; }
      where the Cookies hash returned contains the cookies
      ----
      FightLiteracy.com
      http://www.fightliteracy.com
        When some hosts explicitly remove CGI.pm from the core package, they're plain stupid...
        Parsing cookies is pretty similar to parsing forms, as it can be broken quite easily if you're not carefull. I'm confident your code is flawded in one way or another (altho I'm not gonna chase the hole). Check merlyn's No excuses about not using CGI.pm. I confess that most of my older CGI code didn't use CGI.pm for that kinda stuff, but now I've payed my sin.

        Maybe I should do a use CGI; #damnit! bat too...

        Needless to say I know how to parse cookies manually, since I set it manually in my original node :))

        Greetz
        Beatnik
        ... Quidquid perl dictum sit, altum viditur.
Re: Cookies without using CGI.pm
by lzcd (Pilgrim) on Jan 28, 2001 at 06:02 UTC
    Doing a perldoc CGI reveals that CGI.pm also has a nifty explanation of cookies.
    Those happy little chaps down at O'Rielly also produce two books with decent explanation of cookies:
    'Perl Cookbook' &... err... something regarding Perl and CGI. (No doubt I'll think of its title shortly).

    Update:See floctos guess below for the second book title
      I'm pretty sure that the second book's name is "CGI programming with perl". There's not very much, but a little Sample-Header: Page 32 (2nd edition). Some more detailed information about cookies with cgi.pm can be found at page 286. Have fun :)
      octopus
      --
      GED/CC d-- s:- a--- C++(+++) UL+++ P++++$ L++>++++ E--- W+++@ N o? K? w-- O- M-(+) V? !PS !PE !Y PGP+(++) t-- 5 X+ R+(+++) tv+(++) b++@ DI+() D+ G++ e->+++ h!++ r+(++) y+
Re: Cookies without using CGI.pm
by Monolith-0 (Beadle) on Jan 29, 2001 at 05:31 UTC
    Thank you all for your help.
    Eduardo, I just like learning to program this way. Learning how functions work before I use them so that I can understand them better. I occationally re-invent the wheel for my own purposes just to have a better understaning, and more control over it. However, I will heed what you said and try not to ignore CGI.pm.