Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

cookie check

by Anonymous Monk
on May 15, 2000 at 00:14 UTC ( [id://11536]=perlquestion: print w/replies, xml ) Need Help??

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

is there a way to check if cookies are enabled? i need something like this:
if(!$cookies) { print "cookies must be enabled!"; } else { print "ok"; }

Replies are listed 'Best First'.
Re: cookie check
by snowcrash (Friar) on May 15, 2000 at 11:00 UTC
    Look at the documentation of the CGI module if you want to know how to actually set and check a cookie. it will basicly look like this:

    set a cookie

    #!/usr/bin/perl -w use strict; my $query = CGI->new(); my $cookie = $query->cookie(-name=>'testCookie', -values=>'foo', -expires=>'+1h', -domain=>'.perlmonks.org'); print $query->header(-cookie=>$cookie);

    look if cookie is set

    #!/usr/bin/perl -w use strict; use CGI; my $query = new CGI; my %testcookie = $query->cookie(-name=>'testCookie');
Re: cookie check
by btrott (Parson) on May 15, 2000 at 02:26 UTC
    You can try to set a cookie, using the Set-Cookie header, then redirect to another page that checks for the cookie that you've just tried to set. That's the best way I can think of, right now.

    So just set a cookie, then do a redirect (by sending the Location header, or by using CGI.pm's redirect method) to another page that checks for the cookie in $ENV{COOKIE} (or by using the cookie method of CGI.pm).

Re: cookie check
by guice (Scribe) on May 15, 2000 at 09:20 UTC
    Anonymous is correct. Perl is a server side app, and via the HTTP protocol there is no way for Perl to be able to check anything on the client's machine. It's even unable to check what browser you are running, the web server tells perl that.

    The best that you can do is what btrott said. One way to do it is to set a cookie AND info in the links o the pages, and when a user clicks on a link, you can check to see if there is a cookie or not. If not, keep the user info in the query_string of the links, if so, you can remove the query_string stuff.
    Some thoughts to play with....

    -- philip
    We put the 'K' in kwality!

RE: cookie check
by Anonymous Monk on May 15, 2000 at 06:25 UTC
    As far as I know, since cookies are a browser thing, you'd have to try to set a cookie, and then check if it worked.
Re: cookie check
by Anonymous Monk on May 15, 2000 at 14:31 UTC
    Hello, the cgi-module is a good solution to the problem.

    Why checkt-it just put a warning that if the user doesn't use cookies the folowing of the site doesn't work for him. That's a lot easier !

    I'm not choosing this because it's the easy way out but IMHO is the best way.

Re: cookie check
by athomason (Curate) on May 15, 2000 at 10:55 UTC
    You may be able to use a dirty exploit pioneered by banner ad companies. Browsers accept cookies that are sent along with images as well as with html. So if you sent the cookie with the page you could retrieve it with an image-lookalike script (e.g. one that sent the content-type header of an image but was actually a 1x1 blank pixel). This opens a new can of worms, though, because now you have to figure out how to have the image script tell the original script (with the actual page) whether the cookie was retrieved successfully. I'm not sure if this is possible to do consistently, and it sure wouldn't be easy, so I'd go with btrott's recommendation :-).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found