Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: CGI Session - Refresh Problem

by rhesa (Vicar)
on Apr 24, 2008 at 10:37 UTC ( #682600=note: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://682600]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2020-09-26 21:23 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (142 votes). Check out past polls.