Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

How can I maintain a session cookie?

by yschang (Initiate)
on Feb 02, 2000 at 21:46 UTC ( #2746=categorized question: print w/replies, xml ) Need Help??
Contributed by yschang on Feb 02, 2000 at 21:46 UTC
Q&A  > CGI programming


I would like maintain a session cookie to track users on my site, but the session cookie should be destoryed once the user closes all copies of their browser. How can do it?

Answer: How can I maintain a session cookie
contributed by Anonymous Monk

First of all use the CGI module, it'll make your life easier in general, not just dealing with cookies.

use strict; use CGI qw(:standard); $q = new CGI; # Create a 11 character random string as a # session id. (Yes, it's swiped from the perl cookbook:-) @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 ); $usersession = join("", @chars[ map { rand @chars } (1 .. 11) ]); # Set a cookie that expires when # the user closes their browser $initcookie = $q->cookie( -name=>'User_Session', -value=>$usersession, -expires=>'', # leave this blank -path=>'/', -domain=>'', -secure=>0 ); print $q->header(-cookie=>$initcookie);
For more info on the CGI module, go to: Shawn
Answer: How can I maintain a session cookie
contributed by turnstep

Basically, any cookie that has NO expiration date, or an expiration date in the past (according to the user's system) will not be stored in the cookie.txt (or equivalent) file on the hard drive when the browser exits.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (7)
    As of 2020-10-19 21:19 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (207 votes). Check out past polls.