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

Sorry to bother again, but this program is just too damned hard to debug! I'm writing a login script for the administrative part of the DSGO. But cgi::carp is telling me when I extract my cookie, I can't get any fields. I figure, this is probably because the cookie isnt set, but %cookies is defined, so how do i confirm this so i dont get an error? I think it may be something even worse than that, because if i comment out the part where it makes $users and $pass the cookie fields it gives me an internal error screen, not even a cgi::carp error.

the code is below

#!/usr/bin/perl #1 load my modules use CGI::Carp qw(fatalsToBrowser set_message); use CGI::Cookie; use CGI qw/:standard/; #2 will write variable loader later $domain = "betterwebber.com"; #3 set up error handler BEGIN { sub handle_errors { my $msg = shift; print "<h1>ERROR</h1><br>"; print "message: $msg"; } set_message(\&handle_errors); } #4 fetch cookies %cookies = fetch CGI::Cookie; $user = $cookies{'user'}->value; $pass = $cookies{'pass'}->value; #5 send user to the login page if not logged in $loggedin = &checklogin($user, $pass); &loginpage if $loggedin = 0; #THE MENU if ($loggedin = 1) { print "you are logged in"; } #6 checklogin - sub checking user and pass sub checklogin { my $userdef = $_[0]; my $passdef = $_[1]; open (FILE, "users.dat"); $usergood = 0; foreach $userdata (<FILE>) { ($usertmp, $passtmp, $accessleveltmp, $emailtmp) = split(/''/, $ +userdata); if ((lc($usertmp) eq lc($userdef)) && (crypt($passdef, $passtmp) + eq $passtmp)) { $usergood = 1; } } close (FILE); return ($usergood); } #7 loginpage - sub that logs in user sub loginpage { my $userdef = ""; my $passdef = ""; if (param()) { $userdef = $query->param('user'); $passdef = $query->param('pass'); } if (&checklogin($userdef, $passdef) == 0) { #desplay login html print header, start_html('Login Page'), hr; h1('You are not logged in. Please do.'), start_form, "Username: ", textfield('user'),p, "Password: ", textfield('pass'),p, submit, end_form, hr; print %cookies; } else { #set cookie and display redirect page $user = new CGI::Cookie(-name => 'user', -value => $userdef, -domain => 'betterwebber.com', -secure => 1 ); $pass = new CGI::Cookie(-name => 'pass', -value => $passdef, -domain => 'betterwebber.com', -secure => 1 ); $selfurl = $query->self_url; print header(-cookie=>[$user,$pass]); print start_html('Loggin in...'), h1('Logging in (hang tight)...'), hr; print "<script language='javascript'>top.location.href = '$selfu +rl'</script>" } }

Replies are listed 'Best First'.
Re: Debugging hell!!
by chromatic (Archbishop) on Mar 29, 2003 at 18:52 UTC

    Several small problems are tripping you:

    &loginpage if $loggedin = 0;

    You mean to compare with ==, not assign with = here. loginpage() will never be called because the assignment expression evaluates to 0, or false.

    open (FILE, "users.dat");

    You're not checking to see if the open succeeds. Getting the permissions right on a data file can be tricky, so it's worth dying or warning if you can't open the file.

    if (&checklogin($userdef, $passdef) == 0) {

    This is the second time you're calling this function. It's reasonable to assume you only need to call it once per invocation.

    print header, start_html('Login Page'), hr;

    This should be a comma. The rest of the HTML generating functions do nothing in void context and won't be printed to the screen.

    if (param()) { $userdef = $query->param('user'); $passdef = $query->param('pass'); }

    You're mixing the functional and object oriented interfaces to CGI here. Worse, I don't see where you've defined $query, so this is effectively useless.

    If you turned on warnings with -w or the warnings module and enabled strict, Perl would let you know about most of these errors. Just run it from the command line:

    perl -wc -Mdiagnostics mycgi.pl

    You'll have a lot of things to fix, but if you're having trouble debugging, you need all the information you can get. This is how to let Perl help you.

Re: Debugging hell!!
by dws (Chancellor) on Mar 29, 2003 at 18:24 UTC
    Three thoughts:

    First, merlyn has an article on basic cookie management that you might find useful.

    Next, are you doing this over https:? If not,   -secure => 1 isn't what you want.

    Lastly, you might get more mileage out of creating a CGI instance early in the script. E.g.,

    my $cgi = new CGI(); if ( $cgi->cookie('user') { ... }
Re: Debugging hell!!
by The Mad Hatter (Priest) on Mar 29, 2003 at 16:51 UTC
    You could check to make sure $cookies{'user'} and $cookies{'pass'} are defined and if they aren't, do what you need to do (probably call loginpage and exit since no cookies means they're not logged in, right?).