Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Checking for cookies

by Eagle_f90 (Acolyte)
on Jul 12, 2007 at 16:37 UTC ( [id://626276]=perlquestion: print w/replies, xml ) Need Help??

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

In a perl script I am writing I need to check to see if a cookie already exists on the user's computer and if it does not then create the cookie. I know how to pull values from cookies and make cookies but just can not figure out how to do an IF statement check to look for the cookie. Can anyone give me a hand here?

Replies are listed 'Best First'.
Re: Checking for cookies
by bloonix (Monk) on Jul 12, 2007 at 17:50 UTC
    use CGI; my $cgi = CGI->new(); my @cookies = $cgi->cookie(); print "cookie is set" if @cookies;
    In list context cookie() returns all cookies. This is one way to test if a cookie is set.
Re: Checking for cookies
by rpanman (Scribe) on Jul 12, 2007 at 17:38 UTC
    You know how to pull a value from a cookie, so do that first. If the routine you use to get the value of the cookie does not work then it is likely that the cookie is not set...

    If you post some code then it should be easy to show you how to do this.
      Tried adding an if statment to check value but am still erroring out with "Can't call method "value" on an undefined value at vote.pl line 14." line 14 is the if statment.
      #!/user/perl use CGI q~:standard~; use CGI::Cookie; use CGI::Carp qw(fatalsToBrowser); use strict; use DBI; print "Content-type: text/html\n\n"; my ($dbh, $sth, $filename, $rating, @current, @VotedOn, %cookies, $Can +Vote, $value, $c); $filename = param('name'); $rating = param('rating'); %cookies = fetch CGI::Cookie; $CanVote = 1; @VotedOn = ''; if ($cookies{'FFInfoVotedOn'} -> value) { @VotedOn = $cookies{'FFInfoVotedOn'} -> value; foreach $value (@VotedOn) { if ($value eq $filename) { $CanVote = 0; } } } if ($CanVote) { $dbh = DBI -> connect ('dbi:ODBC:FFmisc', '', '') or die $DBI::err +str; $sth = $dbh -> prepare (qq~select One, Two, Three, Four, Five from + FanRatings where Title = ?~) or die $DBI::errstr; $sth -> execute ($filename) or die $DBI::errstr; @current = $sth -> fetchrow_array; if (!defined $current[0]) { $sth -> $dbh -> prepare (qq~insert FanRatings (Title, One, Two +, Three, Four, Five) values (?, 0, 0, 0, 0, 0)~) or die $DBI::errstr; $sth -> execute ($filename) or die $DBI::errstr; } $sth = $dbh -> prepare (qq~update FanRatings set ? = ? + 1 where T +itle = ?~) or die $DBI::errstr; $sth -> execute ($rating, $rating, $filename) or die $DBI::errstr; @VotedOn = (@VotedOn, $filename); $c = new CGI::Cookie(-name => 'FFInfoVotedOn', -value => @VotedOn, -expires => '+10Y', ); print header(-cookie => $c); print qq~Thank you for rating this fan creation.~; } else { print qq~I am sorry but you have alrady rated this fan creation.~; } print qq~<br />You will automaticly be taken back to the page you came + from in 5 seconds.~;
        Okay, a bit easier to help with the code now. It is probably best to check that the cookie exists before trying to call a method on it (you are getting the error because the cookie does not exist and so perl cannot find relevant "value" method).

        Try this:
        if (exists($cookies{'FFInfoVotedOn'})) {

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://626276]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found