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

Hi, Yesterday I started working on an old program of mine which has a login/membership program.

I tried logging in and it worked and saved the cookies properly. I also tried creating a new user and it too saved the cookies properly, etc -- at least it appeared to be doing these things.

I didn't touch the login program. Last night I wanted to test a different part of the membership program. I logged in like I normally do, and it takes me to the menu page and prints out who I am along with the user id. But if I click on any of the links (ie. update profile, add an event, etc)it says that I am not logged in(!). I tried different user names, etc and I also tried creating a new profile (userid/password) ... it says that I'm not logged in though. I never touched the login program and I checked the settings for cookies on my computer ... everything looks good.

I am attaching the login program and also the subroutine that 'gets' the userid from the cookies.

Any ideas why the program suddenly went bezerk?

The problem must be either the way the cookies are set or the way they are beign retreived and read.

########################################################## ## THIS PROCEDURE MAKES SURE THAT THE USER IS LOGGED IN ########################################################## sub CheckIfUserIsLoggedIn { use CGI::Session; my ($sid,$session,$userid); $cgi=new CGI; $sid=$cgi->cookie("CGISESSID")||undef; $session = new CGI::Session("driver:DB_File",$sid,{Directory=>'/tmp'}) +; $userid = $session->param("userid"); if ($userid eq "") { print "Content-type: text/html\n\n"; print "You are not logged in!! $userid\n\n"; exit; } else { return $userid; } } #################################################### The code below is from the login.pl routine that sets the cookie after + checking that the password and username match what is in the DB: ## If the username and password are correct, then set cookie a +nd proceed if ($r[0] eq $psswd) { $cgi = new CGI; $session = new CGI::Session("driver:DB_File",undef,{Di +rectory=>'/tmp'}); $sid=$session->id(); $session->param("userid",$r[1]); $cookie=$cgi->cookie(CGISESSID=>$session->id); print $cgi->header(-cookie=>$cookie); $sesname=$session->param("userid"); $content = &Get_Account_Manager_html($r[1],$r[2],$r[3] +); ## we pass the userid $content = "<b>userid:$sesname</b>".$content; &PrintWebpage($content,"yes"); }

Replies are listed 'Best First'.
Re: Problem with cookies
by moot (Chaplain) on Feb 28, 2005 at 22:11 UTC
    General trouble-shooting tips:
    1. Sanity dictates that if "nothing changed", and something broke, then something was changed.
    2. Occam uses his Razor more often than not.
    3. For the sake of argument, assume the code is ok (but see rule 1). So that means something is wrong with the environment. Check that first, for example, check in your browser that the cookie really is being set.
    4. If the environment really looks ok, check it again.
    5. If it still looks ok, have someone else check it.
    6. If it still looks ok, are you sure there is no loose nut on the keyboard, and that PEBKAC does not apply? Also check for ID-ten-T errors.
    7. If you still can't find a problem (and still bearing in mind rule 1), check the code. Use general debugging techniques to narrow down the problem. Run your unit tests and verify they still complete successfully.
    8. Avoid absolute statements like "the problem must be with the frobnitz confrogulator". These blind you to avenues of investigation you might otherwise explore.
    Given the nature of your problem, and if you are really sure the code was not changed, most likely something with the browser or the user is not what you expect. You would need to post more of your code, including the login.pl script, for anyone here to be able to debug it.

    Update: added absolute statements tip.

Re: Problem with cookies
by tphyahoo (Vicar) on Mar 01, 2005 at 10:12 UTC
    I've struggled with logins as well. You might want to try using HTTP::Cookies. I had a problem that went away when I did things with HTTP::Cookies, though I don't really understand why. FWIW:
    use strict; use warnings; use HTTP::Request::Common; # HTTP handling use HTTP::Headers; use HTTP::Cookies; $cookie_jar->clear; my $cookie_jar = adwordsLogin($adwordsLogin{login},$adwordsLogin{passw +ord},$adwordsLoginWait,$options{language},$proxy); my $ua = LWP::UserAgent->new; $ua->cookie_jar( $cookie_jar );