Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

rechecking the cookie

by Hammy (Scribe)
on Mar 28, 2001 at 00:32 UTC ( [id://67627]=perlquestion: print w/replies, xml ) Need Help??

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

How can I tell if a site either does not accept cookies or has been given the option to accept my cookie and has declined. I have the following code that works great - but I've run into a problem with those that choose to forgo my cookie.
sub cookietest{ my $cookname = "newcookie"; my $cookietest = CGI->cookie($cookname); unless (defined $cookietest) { my $cookie1 = CGI->cookie(-NAME=>$cookname, -VALUE=>"abc", -EXPIRES=>"+2y", -PATH=>"/"); print CGI->header(-COOKIE => $cookie1); } }
I don't know how to re-request the cookie. Just redoing
  my $cookietest = CGI->cookie($cookname);
does not work because I've already gotten the page.
Thanks in advance!

Replies are listed 'Best First'.
Re: rechecking the cookie
by darobin (Monk) on Mar 28, 2001 at 00:42 UTC

    I'm not sure I understand your question properly. Your code and part of your prose indicates that you are sending cookies to people visiting your site, but other parts of your prose seem to say that you are getting a cookie from a site (eg with LWP). I'll assume the former as it has 80% probability :)

    It pretty much depends on whether cookies are vital to your app. If they are, when someone hits a part of it and doesn't have a cookie it means that they just arrived. What I do in such cases is set the cookie and redirect to another URL (eg. /cookie-check.pl). Then, at that other URL, I check that I get my cookie back. If it's there, then all's perfect and I redirect them back to where they wanted to go to in the first place. If it isn't, then I display an error page stating that cookies are needed.

    If your app doesn't absolutely need cookies to work, then don't bother.

    HTH -- darobin

Re: rechecking the cookie
by stephen (Priest) on Mar 28, 2001 at 00:46 UTC
    Check out the previous discussion on cookie check. The general pattern is:
    1. Try to set the cookie.
    2. Redirect to your current script, passing a parameter indicating that you have attempted to set the cookie.
    3. Have an if-then in your script watch for the presence of this parameter. If it's set and there is no cookie, then the client has cookies turned off.

    Don't forget, though, that since clients can approve or disapprove of cookies individually, the fact that they allow one cookie to be set doesn't mean that they'll allow others.

    stephen

      stephen's words regarding individual approval/disapproval are true, so make sure that you test each cookie for acceptance. Better yet, limit cookies to one per user and maintain the required data (related to that cookie and user) on the back-end.
Re: rechecking the cookie
by Masem (Monsignor) on Mar 28, 2001 at 00:44 UTC
    I do not think there is a way to do this..cookies are negotiated when http headers are sent out and there's no other way to access them. In addition, the http protocol doesn't have any way to determine if the user accepted the cookie or not.

    So if you really want to know if they rejected your cookie (and remember, it may not be the user directly that rejected it but proxy software upstream), you can store the cookie that you sent them with some identifying info, and then doublecheck against that info when they re-identify to the site; if you don't get a cookie back from them, you can say something to that effect.

    But otherwise, either you tell the user that they need to enable cookies to proceed, or work out some other way of authenication for the site when cookies fail (such as authorization codes).


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: rechecking the cookie
by sutch (Curate) on Mar 28, 2001 at 00:43 UTC
      I knew that the question seemed like deja vu, but now that I see that Server getting and utilizing cookie was also asked by the same monk 26 days ago, I wonder if I'm either overdosed on caffine or if I just don't understand Hammy's latest question.
        FYI: The answer that I got to my previous cookie question (26 days ago) worked wonderfully. I have a script that has to set a cookie and then check to see if the cookie took before proceeding. I never got the answer to the last part of how to recheck. It seems the response from darobin is the last piece to my puzzle. Thanks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found