Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Cookie question

by mstuder (Initiate)
on Dec 08, 2007 at 00:35 UTC ( [id://655784]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to set a cookie in the middle of my web page. Here is a testcase showing a problem that I believe is happening to me in my program. This code WORKS:
#!/usr/bin/perl -w use strict; use CGI qw/:standard/; print header( -type => 'text/plain', -cookie => cookie( -name=>'ident', -value=>'cookie#1') ); print "Cookie SET";
And this will NOT work:
#!/usr/bin/perl -w use strict; use CGI qw/:standard/; print "Trying to set a cookie\n"; print header( -type => 'text/plain', -cookie => cookie( -name=>'ident', -value=>'cookie#1') ); print "Cookie SET";
I need a way to make this work. Maybe spawn off some sort of separate process to do it? I suspect many people are having a similar problem. M

Replies are listed 'Best First'.
Re: Cookie question
by grep (Monsignor) on Dec 08, 2007 at 00:56 UTC
    You can do whatever you want before printing the header as just long as long as you don't print to STDOUT.

    The first thing the webserver wants to see is a header, before it sends your page. If it doesn't then there is a problem. Everything sent out STDOUT goes to the webserver, so either print to STDOUT after the header or print to another file handle.
    (I also recommend using the object interface of CGI)

    So do this (untested):

    #!/usr/bin/perl -w use strict; use CGI; my $q = CGI->new(); open LOG,'>','logfile' or die "can't open logfile\n"; print LOG "Trying to set a cookie\n"; print header( -type => 'text/plain', -cookie => $q->cookie( -name=>'ident', -value=>'cookie#1') ); print "Cookie SET";
    OR
    #!/usr/bin/perl -w use strict; use CGI; my $q = CGI->new(); my $bar = do_my_cookie_stuff( $q->param('foo') ); print header( -type => 'text/plain', -cookie => $q->cookie( -name=>'ident', -value=>$bar ) ); print "Cookie SET";
    grep
    One dead unjugged rabbit fish later...
Re: Cookie question
by Fletch (Bishop) on Dec 08, 2007 at 00:49 UTC

    Cookies are set in the headers which have to be sent to the web browser before anything else, so of course that doesn't work.

    Scent of an XY Problem aside, consider rearranging the order you do things in your code. Do all your computation / frobnication / examination of inputs first, then send whatever reply you're going to send (including the conditional cookie) at the end.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Cookie question
by Cubes (Pilgrim) on Dec 08, 2007 at 11:51 UTC
    If, for some reason, you absolutely have to send part of your HTML output before setting the cookie, you can put some javascript later in the page that sets a cookie on the fly.

    This may come with its own set of problems, though, so rearranging things to put all of your output at the end probably is a better solution.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2024-10-10 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (45 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.