Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Setting and Reading Cookies

by gryphon (Abbot)
on Jun 21, 2005 at 16:45 UTC ( #468749=note: print w/replies, xml ) Need Help??


in reply to Re^2: Setting and Reading Cookies
in thread Setting and Reading Cookies

Greetings rjsaulakh,

Cookies are bits of text stored on the user's browser, which means that to "set" (write, save, put, etc.) a cookie, you have to send something to the browser. The browser sends cookie data (that you're allowed to view) in the HTTP header during a page request, which means that to "get" (read, fetch, etc.) a cookie, you have to have the browser send you something. In the world of CGI scripts, you have to get something from the browser (the request) before you can send something to the browser (the response). So you can't both set and read a cookie in the same script in the same request/response event. You have to first write the cookie in one response, then and only thereafter can you read the cookie.

Here's an example:

#!/usr/bin/perl use strict; use warnings; use CGI; my $cgi = CGI->new; my $cookie = $cgi->cookie( -name => 'sithLord', -value => 'Darth Vadar' ); print $cgi->header( -cookie => $cookie ); my $cookie_data = $cgi->cookie('sithLord') || 'No Cookie Set'; print "<h2>Cookie Data: $cookie_data</h2>\n";

The first time you hit this CGI, you'll get "Cookie Data: No Cookie Set." However, reload the page and you'll get "Cookie Data: Darth Vadar."

gryphon
Whitepages.com Development Manager (DSMS)
code('Perl') || die;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://468749]
help
Chatterbox?
[marioroy]: Hello all. After several attempts, am able to move forward and will soon release a Parallel:: ForkManager compatible MCE::Hobo:: Manager, also MCE::Hobo::Simple, and refactored MCE::Hobo engine supporting multiple instances. Wait works beautifully.
[marioroy]: MCE::Shared has been refined that it allows sharing Tie::File and hash objects containing {fh} key.
[marioroy]: It's taken so long to take Hobo to a new level. Threads-like and Parallel:: ForkManager management capaiblities.
[marioroy]: This will complete the 4 years in the making and likely time to move on to something els.
[marioroy]: s/els/else.
[marioroy]: The MCE::Hobo::Pool module will come later.

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-05-26 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?