I seem to get this error and cant figure out why, i think its something to do with the header. Its also not assigning a cookie/session, so I know im doing something wrong but cant figure it out. Error: Name "main::cookie" used only once.
#!/usr/bin/perl -w use CGI; use HTML::Template; use CGI::Session; use lib ('/home/scott/intranet/cgi/mods'); use Digest::SHA2; use DBI; use DBD::mysql; CGI::Session->name("test"); my $query = new CGI; my $sid = $query->cookie( 'test' ) || undef; my $session = CGI::Session->new("driver:File", $sid, {Directory=>' +/tmp'}); $query->param( 'session' => $session); if (!$sid or $sid ne $session->id ) { my $cookie = $query->cookie( -name => 'test', -value => $session->id, #-expires => '' makes it so on close session expires. ) } init($session, $query); sub init { my ($session, $cgi) = @_; # receive two args if ( $session->param("~logged-in") ) { return 1; # if logged in, don't bother going further } my $lg_name = $cgi->param("lg_nick") or return; my $lg_psswd=$cgi->param("lg_pass") or return; # if we came this far, user did submit the login form # so let's try to load his/her profile if name/psswds match if ( my $profile = login($lg_name, $lg_psswd) ) { $session->param("~profile", $profile); $session->param("~logged-in", 1); $template->param(PROFILE => 1); $session->clear(["~login-trials"]); return 1; } # if we came this far, the login/psswds do not match # the entries in the database my $trials = $session->param("~login-trials") || 0; return $session->param("~login-trials", ++$trials); } my $template = HTML::Template->new(filename => 'index.tmpl', path => '/home/scott/intranet/cgi/ +intranet/', associate => $query); sub getpassword { # MySQL CONFIG VARIABLES my($username) = @_; my $host = "localhost"; my $database = "write"; my $user = "userid"; my $pw = "dbpass"; my $dsn = "dbi:mysql:$database:localhost:3306"; my $dbstore = DBI->connect($dsn, $user, $pw) or die "Unable to con +nect: $DBI::errstr\n"; my $prepquery = $dbstore->prepare("SELECT * FROM userlogin WHERE U +SERNAME='$username'") or die "Unable to connect: $DBI::errstr\n"; $prepquery->execute(); my $timestampUpdate = $dbstore->prepare("UPDATE write.userlogin SE +T userlogin.TIMESTAMP = NOW( ) WHERE userlogin.USERNAME = '$username' +" ) or die "Unable to connect: $DBI::errstr\n"; $timestampUpdate->execute(); my $ref = $prepquery->fetchrow_hashref(); my $password = $ref->{'PASSWORD'}; $prepquery->finish(); $timestampUpdate->finish(); $dbstore->disconnect(); return $password; } sub login{ my($nick, $pass) = @_; my($password) = getpassword($nick); my $encryptobj = new Digest::SHA2 512; $encryptobj->add($pass); my $digest = $encryptobj->hexdigest(); if($digest eq $password){ # replace this check above with something real ie lookup f +rom a database return 0; } else { return 1; } } $template->param('TEST', $query->param('lg_nick')); $template->param(MYURL => 'http://192.168.1.9/cgi-bin'); print $query->header( -cookie=>$cookie ), $template->output;

In reply to CGI::Session error saying main::cookie used only once? by tacogrande

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.