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

Perl + web cookies, not sure why this is failing

by desertrat (Acolyte)
on Mar 03, 2012 at 17:30 UTC ( #957697=perlquestion: print w/replies, xml ) Need Help??
desertrat has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a web app that requires a certain cookie to be set, and it seems like setting the cookie is taking place AFTER the web script checks for it. The first time the web page is loaded (and this has been tested in three different web browsers) the script cannot find the cookie, even though if you look (for instance, in Web Inspector in Chrome) the cookie is clearly set.

If you reload the page, then the cookie is found, of course, but Im setting the cookie before I ever send anything to the browser, so why isn't it found immediately? At first I tried putting the check and setting of the cookie in a perl module that's called by all of the scripts involved, but that didn't work. Thinking that this had something to do with mod_perl, and the module only being compiled once, I made it a sub in the module and call the sub in the script itself.

That produces the same result. Running it on a non-mod-perl apache instance also produces the same result. I"ve got a demo code that will do this, a .pm file and .pl file that's actually run as the cgi script.

A test page is here

It will display the returned result from running the cookie-setting sub, then display the result of the check.

Here's the code :

use strict; #use warnings; use CGI qw(param :standard); require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw($MyCookie $PrintHeader CheckCookie) + ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw($MyCookie $PrintHeader CheckCookie); our $VERSION = '1.0'; our $PrintHeader = "Content-type: text/html\n\n"; our $MyCookie = "ARandomCookie"; sub CheckCookie { my $affil = cookie($MyCookie); if ($affil eq ''){ $affil = "It Should Be Set!!"; my $packedURLcookie = cookie( -NAME => $MyCookie, -VALUE => "$affil", -path => "/"); print "Set-Cookie: $packedURLcookie\n"; return 1; } } 1;

And the actual script:

#!/usr/bin/perl use strict; use CGI qw(param :standard); use CookTest; my $subres = &CheckCookie; my $alleged = cookie($MyCookie); print $PrintHeader; print <<EOF; <html> <title>Cookie tester</title> <body> My sub cal results are $subres <p> My alleged cookie is $alleged <p> </body> </html> EOF exit;

Replies are listed 'Best First'.
Re: Perl + web cookies, not sure why this is failing
by tobyink (Abbot) on Mar 04, 2012 at 01:32 UTC

    What you describe is expected behaviour. On the request where you set the cookie, you cannot also read the cookie. That's just how cookies work.

    This is not Perl-specific: you'll see the same result writing CGI scripts in pretty much any language; you'll see the same in PHP, the same in Ruby on Rails.

      Thank you. Gah..I was trying to avoid doing a page reload here, as the main page people will be hitting is kind of slow to start with, and doing it twice over will just make it that much more annoying. Looks like I'll have to do this via some ajax code; more complicated to write.

        You don't need to do any such thing. If your application is setting a cookie, then it doesn't need to be able read it back.

        Imagine I've posted you a letter. I don't need to wait until you write back to find out what I wrote to you. I wrote it. I can remember.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957697]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-05-25 11:54 GMT
Find Nodes?
    Voting Booth?