http://qs321.pair.com?node_id=682600


in reply to CGI Session - Refresh Problem

You only get one chance to print the HTTP headers, so your second print $cgi->header isn't going to be effective.

The solution is to first create/retrieve the session, then bake the cookie, and only then print the header:

#!/perl/bin/perl -wT use strict; use CGI; use CGI::Session; my $cgi = CGI->new; my $session = CGI::Session->new( undef, $cgi, {Directory=>'/tmp'} +); my $cookie = $cgi->cookie(CGISESSID => $session->id ); print $cgi->header(-cookie=>$cookie); print $cgi->start_html("query.cgi"); print "cookies is : $cookie <BR>" ; my $sid = $session->id(); print "<BR>sid = $sid <BR>"; print $cgi->end_html;

Replies are listed 'Best First'.
Re^2: CGI Session - Refresh Problem
by d0353101 (Novice) on Apr 25, 2008 at 05:32 UTC
    Dear Rhesa, Many thanks for making this correction !! It worked like wonder.

    It resolved one query but still same Old questions are there:
    1. I am having administrator privilleges. On which path cookies are being stored. Cookies are not being stored at "C:\Documents and Settings\Administrator\Cookies"

    2. ( I do NOT want to use cookie)
    If I press refresh button on browser for "query.cgi" it creates a new session. I want the same session after refresh button is pressed. ( I do NOT want to use cookie) Please help me to achieve this.
      Those questions are answered in the manual, as well as in the excellent tutorial.

      1. You set the directory where sessions are stored with the Dir parameter;
      2. Instead of printing $cgi->header(...), you can also use $session->header(). That contains the logic for whether you need a new cookie or not.