Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

CGI SESSION Cookies and SSI help

by seekperlwisdom (Acolyte)
on Dec 15, 2009 at 21:56 UTC ( [id://812942]=perlquestion: print w/replies, xml ) Need Help??

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/'; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://812942]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2026-03-11 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.