You're being a little inconsistent in your code postings. For example is the cookie name 'id' or 'login'?

Why are you using fetchrow instead of fetchrow_array? The former is an old alias for the later and even was removed from the documentation. To learn of it, you need to search the module's source code or have old documentation.

Why are you assigning $x but never use it? BTW, the author recommends against calling fetchrow (or fetchrow_array) in scalar context. You should call it in list context. And most importantly, why aren't you using the strict and warnings pragmas?

Here's an untested rewrite of your 2 scripts which should get you closer to your goal.

Edit: oops - I made a couple updates to the posted script (i.e., I moved the db stuff to a sub) and poj spotted a goof on my part in the placement of $welcome and $login var assignments. Should be fixed now.

home.pl

use strict; use warnings; use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); my $cgi = CGI->new(); my $username = $cgi->param('username'); my $password = $cgi->param('password'); my $id = get_uid($username, $password); my $welcome = 'http://localhost/cgi-bin/welcome.pl'; my $login = 'http://localhost/cgi-bin/login.pl'; if ($id) { my $cookie = $cgi->cookie(-name => 'login', -value => $username); print $cgi->redirect(-location => $welcome, -cookie => $cookie); } else { print $cgi->redirect($login); } exit; sub get_uid { my $username = shift; my $password = shift; my $db = 'datab'; my $usr = 'root'; my $pwd = ''; my $host = 'localhost'; my $dbh = DBI->connect("DBI:mysql:$db:$host", $usr, $pwd, {AutoCommit => 0, RaiseError => 1}) or die $DBI::errstr; my $sth = $dbh->prepare("SELECT id FROM mysql_auth WHERE username = ? AND password = ?"); $sth->execute($username, $password); my ($id) = $sth->fetchrow_array; $sth->finish(); return $id; }

welcome.pl

use strict; use warnings; use DBI; use CGI; use CGI::Carp qw(fatalsToBrowser); my $cgi = CGI->new(); my $sid = $cgi->cookie('login'); my $login = 'http://localhost/cgi-bin/login.pl'; if ( ! $sid ){ # exit and return to login.pl! we have no cookies print $cgi->redirect($login); } #start using the page! we have cookies print $cgi->header(); # etc


In reply to Re^12: cookie problem by fishmonger
in thread cookie problem by bigup401

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.