Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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;


In reply to Re^3: Setting and Reading Cookies by gryphon
in thread Setting and Reading Cookies by bigjoe11a

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found