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

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://957697]
Approved by Corion
help
Chatterbox?
[1nickt]: This is not to suggest that Karl should remove his slogan. Just thoughts of mine... Trust me I believe I am very much more radical than most, and if I had a slogan for every gross injustice in my sig, there would be no more bytes!
Discipulus redskins .. well native american..
[1nickt]: So if I was to offer any editorial advice it would be to (1) make it endure time passing (2) make it positive (3) make it about you and your beliefs
[1nickt]: (Disicpulus The new best choice is maybe "First People"
[karlgoethebier]: Discipulus: This is called "Volksverhetzung" in German which means
[karlgoethebier]: incitement of the people and it is prohibited by law
[1nickt]: The best sign I have sign in a protest march was carried by a kid of about 10 years "If you build a wall my generation will tear it down."
[1nickt]: ... sign I have seen ...
[1nickt]: Now that one made me hopeful! First time not angry in a long time when I saw that kid.
[Discipulus]: i propend for removing: why? because we are so few that we must find i minimal common divisor, this is certainly Perl not our (anyway private) thougths. And i say this still wondering because i love a lot freedom of expression. And i say this not for roho

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2017-06-22 12:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (519 votes). Check out past polls.