koryw has asked for the wisdom of the Perl Monks concerning the following question:

Hi, If you could help me I would appreciate it. The following code below creates a session and cookie. The problem is it never initializes the session again, it always creates a new session and cookie every time. Do you see anything I'm missing?
#!/opt/bin/perl #use strict; use CGI::Carp; use CGI; use File::Spec; use CGI::Session; use Date::Manip; #use CGI::Session::DB_File; print "Content-type: text/html\n\n"; my $cgi = new CGI(); my $session = new CGI::Session(undef, $cgi, {Directory=>File::Spec->tmpdir }); my $sweet = $cgi->cookie($session->name); if ($sweet) { print "cookie exists $sweet<BR>"; my $ipa = $session->param("_SESSION_REMOTE_ADDR"); my $i = $session->param("NVISITS") || 0; #If Statement I can use f +or other values $session->param("NVISITS", ++$i); my $current=&ParseDate("today"); my $d = $session->param("Dateid") || $current; #If Statement I can use for other values $session->param("Dateid", "$current"); $session->expire('+1m'); print $cgi->start_html(-title=>"Session"), $cgi->h1("Session Information"), #$cgi->h1("Hello World from CGI::Session/$CGI::Session::VERSION"), $cgi->p("This is CGI::Session $CGI::Session::VERSION"), $cgi->p("Your session id is <strong>" . $session->id . "</strong>"); print $cgi->div("You visited this page $i times so far, right? Date $d IP Address: $ipa $sweet<BR>"); #print $cgi->div("You visited this page $i times so far, right? Date $d IP Address: $ipa cookie session: $sweet<BR>"); #print $cgi->p("Cookie Session: $getcookie"); #print $cgi->end_html(); }else{ print "New cookie set<BR>"; #Building a cookie to be sent to user my $cookie = $cgi->cookie(-name=>$session->name, -value=>$session->id, -expires=>'+1m'); # Setting cookie in the Browser: print $cgi->header(-cookie=>$cookie); #print $cgi->header(-type=>'text/html', -cookie=>$cookie); #print $cgi->header(-cookie=>$cookie); my $ipa = $session->param("_SESSION_REMOTE_ADDR"); my $i = $session->param("NVISITS") || 0; #If Statement I can use f +or other values $session->param("NVISITS", ++$i); my $current=&ParseDate("today"); my $d = $session->param("Dateid") || $current; #If Statement I ca +n use for other values $session->param("Dateid", "$current"); $session->expire('+1m'); } exit;
-- Kory Wheatley Academic Computing Analyst Sr. Phone 282-3874 ######################################### Everything must point to him.

Replies are listed 'Best First'.
Re: Perl Cgi Session cookie initializing
by pfaut (Priest) on Mar 10, 2003 at 21:43 UTC

    You are setting an expiration time of one minute. Are you sure your machine's clock is set that close to the server's clock? Your cookie could easily be expired before you get it. Try increasing the expiration time and see if that helps.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
      Nope. Same result, I've tested cookies before setting the expiration one minute apart and click reload to test and its always retrieved the cookie that was set. Any other suggestions.
Re: Perl Cgi Session cookie initializing
by Hero Zzyzzx (Curate) on Mar 11, 2003 at 00:47 UTC

    You're printing two "Content-type" headers. Remember, the FIRST thing your script has to emit is a valid Content-type header, one that includes the cookie if you're trying to set one.

    Fix your header problems and your script should work fine.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      Thanks that was the problem. I appreciate your response.