Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Setting and Reading Cookies

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


in reply to Setting and Reading Cookies

Greetings bigjoe11a,

I recommend you read through the CGI.pm manual's section on cookies. To write a cookie, just print the header of a page with a cookie object.

use strict; use CGI; my $cgi = new CGI; my $cookie = $cgi->cookie( -name => 'cookieName', -value => 'cookieValue', -expires => '+1h' ); print $cgi->header( -cookie => $cookie );

To read the cookie, just use this:

my $value = $cgi->cookie('cookieName');

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


Comment on Re: Setting and Reading Cookies
Select or Download Code
Re^2: Setting and Reading Cookies
by bigjoe11a (Novice) on Jun 21, 2005 at 02:50 UTC
    Ok, I get your idea for your code, I have no idea on what CGI.pm is. Thanks Joe

      Greetings bigjoe11a,

      The CGI module (or CGI.pm to distinguish it from the CGI protocol) is a very core Perl module that helps write CGI scripts and other CGI-like things. For very simple scripts, CGI.pm is a bit overkill, but it's very helpful and useful for anything substantial.

      You can use the module to create Web fill-out forms, do stuff with form content, read/set cookies, muck around with HTTP headers, create HTML... Oh gosh, just read the POD. If you're writing CGI scripts in Perl, use CGI.pm unless you have a good reason not to.

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

        Hay, thanks for the info, I just wanted to tel you that the part you game me to read my cookies works great. I can't under why I can't set any more then one cookie. have any ideas. print $cgi->header( -cookie => $cookie1,$cookie2,$cookie3) This my line that I use. It just doesn't set cookie2 or cookie 3. Thanks Joe
Re^2: Setting and Reading Cookies
by rjsaulakh (Beadle) on Jun 21, 2005 at 05:35 UTC
    Hi gryphon
    i have read numerous posts about cookie and session management with cookies . But still i have some queries
    1. u can read a cookie only when after u fetch a cookie and u can fetch a cookie like this
    %cookies = fetch(CGI::Cookie)
    2. how do u make sure that cookie has actually been sent .

    the basic problem i am facing is that inspite of setting the cookie properly i am not able to fetch the cookie .i want to debug where i am going wrong .can you please guide me

      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://468547]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-07-23 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (154 votes), past polls