#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    79
    Rep Power
    15

    Having trouble with CGI cookies


    Im trying to use cookies to store the a $nick value from a login script so that I can use it to query a mysql database later on.
    I wrote 2 subroutines that are supposed to deal with this.
    one is..

    Code:
     sub createCookie
    {
    	my $nick=shift;
    	my $cookie = cookie(-name=>'nick',
                     -value=>'$nick',
                     -expires=>'+1h',
                     -path=>'/',
                     -domain=>'.wiu.edu/users/scg103',
                     -secure=>0);
    print header(-cookie=>$cookie);
    }
    That creates the cookie during the login script, i call it using
    createCookie($nick);
    So far the script executes fine. I don't know whether the cookie is being stored though.

    Another script calls for the cookie before a mysql query so i could do something like

    Code:
    my $nickname = grabCookie();
    my $query = $db->prepare("select * from users where nick = '$nickname';");
    $query->execute;
    my($nick,$pass,$rname,$email,$phone,$sex,$icq,$aim,$avatar,$info,$uid,$banned,$age) = $query->fetchrow_array;
    The subroutine that calls the cookie "GrabCookie" looks like this.
    Code:
    sub grabCookie
    {
                  $q = new (CGI);
    	my $nick = $q->cookie('nick');
    	return $nick;
    }
    Although i have also tried this

    Code:
    sub grabCookie
    {
    	
    	my $nick = cookie('nick');
    	return $nick;
    }
    When the grabCookie sub gets called, it doesn't give $nick any value and all the mysql data goes unitialized. If anybody could help me, i would really appreciate it.
  2. #2
  3. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Posts
    1,137
    Rep Power
    177

    Re: Having trouble with CGI cookies


    Originally posted by deadbabypizza
    I don't know whether the cookie is being stored though.
    Mozilla is a good tool to view whether cookies are getting passed to your browser. Mozilla has an excellent cookie viewer under:

    > Edit > Preferences > Privacy & Security > Cookies > Managed Stored Cookies

    It parses the cookies and shows you all the data. Or you can just dig into Mozilla's cookies.txt file. You can also set it up to prompt you each time your site attempts to set a cookie.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    79
    Rep Power
    15
    Much thanks, It looks like no cookies are being passed. But I still don't know why a cookie isn't being made.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    28
    Rep Power
    0
    Just a thought, I am not sure of this but I quote my values differently.

    my $cookie = cookie(-name=>'nick',
    -value=>'$nick',

    I would double quote this: cookie(-name=>"nick",
    and not single quote the value line: -value=>$nick,

    Cheers,
    Sean
  8. #5
  9. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    87
    As a clarification: single quotes are literal strings with NO variable interpolation, double quotes interpolate variables within them.

    If you're just putting a scalar in a method's attribute, you don't need to quote it. Quotes don't do anything magical.

    Technically, if you're putting up a value that doesn't require variable interpolation (like 'nick') single quotes are faster, but I wouldn't get too excited if you've been using double quotes.

    I don't know if your domain is correct, but a lot of times cookie problems can be traced to this. Usually I set mine to '/', as I don't store anything sensitive in cookies.

    Another cookie trap- you have to set the cookie with a full CGI transaction before you can retrieve it.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    79
    Rep Power
    15
    Thank you guys, I can't believe that i didn't realize it was $nick was in single quotes.
    I found out the other part of the problem, i used the wrong syntax.
    This is what im using now.

    Code:
    my $q = new CGI;
    	my $cookie = cookie(-name=>'nick',
                     -value=>$nick,
                     -expires=>'+1d',
                     -path=>'/',
                     -domain=>'.wiu.edu/users/scg103',
                     -secure=>0);
    print $q->header(-cookie=>'nick');
    I was printing "$cookie" instead of the name for the cookie. Using mozillas cookie manager really helped alot too. Much appreciated guys.

IMN logo majestic logo threadwatch logo seochat tools logo