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

Thanks for reply but i just find out if i run my code as http://localhost/cgi-bin/script.pl, cookie function work. my file is accessed by SSI. but this way there is no cookie. I dont know either it is script or my apache server. thanks
#!/usr/bin/perl -w use strict; use CGI; use DBI; use Template; use CGI::Ajax; use CGI::Session; use CGI::Cookie; use CGI::Session::Driver::mysql; use CGI::Carp qw(fatalsToBrowser); my ($src, $lib, $files); #print "Content-type:text/html\n\n"; my $cgi = CGI->new(); my $ip = $cgi->remote_host(); my $sid = $cgi->cookie("CGISESSID") || undef; my $os = $^O; if($os =~ /MSWin32/){ $src = 'D:/mixedapache/cgies/templatetoolkit/src'; $lib = 'D:/mixedapache/cgies/templatetoolkit/lib'; $files = 'D:/mixedapache/cgies/templatetoolkit/'; }else{ $src = '/mnt/win_d/mixedapache/cgies/templatetoolkit/src'; $lib = '/mnt/win_d/mixedapache/cgies/templatetoolkit/lib'; $files = '/mnt/win_d/mixedapache/cgies/templatetoolkit/'; } my $dbh = DBI->connect('dbi:mysql:phones', 'name', 'password', {RaiseE +rror =>1}); my $session = CGI::Session->new('driver:mysql', $sid, { TableNAme => 'sessions', IdColNAme => 'id', DataColName=> 'a_session', Handle => $dbh, }) or die CGI::Session->errstr; my $cookie = $cgi->new(-name => $session->name, -value => $session->id +); print $session->header(); my $config = { INCLUDE_PATH => [ $src, $lib, $files, ] }; my $tt = Template->new($config); my $url ='/cgi-bin/ajax.pl'; my $ajaxbit = CGI::Ajax->new( 'external' => $url, 'login' => \&login, 'skip_header' => 1, ); $ajaxbit->skip_header(1); print $ajaxbit->build_html($cgi, \&mainpage); sub mainpage{ my $output = ''; my $vars = { }; my $input = 'perltemp'; $tt->process($input, $vars, \$output) || die $tt->error( ); return $output; }

Replies are listed 'Best First'.
Re: CGI SESSION Cookies help
by Khen1950fx (Canon) on Dec 16, 2009 at 02:01 UTC
    Your script has some errors, but, looking at setting cookies, I used the example in the docs. This is a simplified version, but you might want to try it something like this (shortened-version):

    #!/usr/bin/perl use strict; use warnings; use CGI qw/:standard/; use DBI; use Template; use CGI::Ajax; use CGI::Session; use CGI::Cookie; use CGI::Session::Driver::mysql; use CGI::Carp qw(fatalsToBrowser); my ( $src, $lib, $files ); my $cgi = new CGI; my $ip = $cgi->remote_host; my $c = new CGI::Cookie(-name => 'foo', -value => ['bar','baz'], -expires => '+3m'); print "Set-Cookie: $c\n"; print "Content-Type: text/html\n\n"; my $os = "$^0"; if ( $os =~ /MSWin32/ ) { $src = 'D:/mixedapache/cgies/templatetoolkit/src'; $lib = 'D:/mixedapache/cgies/templatetoolkit/lib'; $files = 'D:/mixedapache/cgies/templatetoolkit/'; } else { $src = '/mnt/win_d/mixedapache/cgies/templatetoolkit/src'; $lib = '/mnt/win_d/mixedapache/cgies/templatetoolkit/lib'; $files = '/mnt/win_d/mixedapache/cgies/templatetoolkit/'; }