Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How can I maintain a session cookie?

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


Description:

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=>'www.somewebsite.com', -secure=>0 ); print $q->header(-cookie=>$initcookie);
For more info on the CGI module, go to: http://stein.cshl.org/~lstein/ 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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (10)
    As of 2014-08-28 01:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (255 votes), past polls