in reply to Re: Cookies write to screen, not to cookie file
in thread Cookies write to screen, not to cookie file

As I mentioned, I've been working on this one problem for days. I've tried to use CGI::Cookie, copying the example directly from the CPAN page. I didn't care what data I wrote to the cookie file file, as long as I wrote something. Here's the script I used:
#!/usr/bin/perl #################################### use CGI qw/:standard/; use CGI::Cookie; sub write_cookie { my $c = new CGI::Cookie(-name => 'foo', -value => ['bar','baz'], -expires => '+3M' ); print header(-cookie=>[$c]); %cookies = fetch CGI::Cookie; $id = $cookies{'ID'}->value; %cookies = parse CGI::Cookie($ENV{COOKIE}); } 1;
This is what displays on the screen:

Set-Cookie: foo=bar&baz; path=/; expires=Wed, 29-Sep-2004 19:22:16 GMT Date: Thu, 01 Jul 2004 19:22:16 GMT Content-Type: text/html; charset=ISO-8859-1

The rest of the page is blank. Since this was even worse that what I was getting before, I returned to work with my former code.

I also tried CGI, using an example I was able to find:
#!/usr/bin/perl #################################### use CGI; $cgiobject=new CGI; $cgiobject->use_named_parameters; &get_state_variables; $cookie_data=&prepare_cookie; &set_cookie($cookie_data); return; sub get_state_variables() #retrieve from the CGI queries the keys and +value we want to store in the cookie { $b_first=$cgiobject->param("reg_custnamef"); $b_mid=$cgiobject->param("reg_custnamemi"); $b_last=$cgiobject->param("reg_custnamel"); } sub prepare_cookie() #packages the variables into one data string +for storage in cookie { $cookie_data="b_first=$b_first|". "b_mid=$b_mid|". "b_last=$b_last"; return $cookie_data; } sub set_cookie($cookie_data) #sets cookie on user's machine { $final_cookie=$cgiobject->cookie(-name=>'searchform', -value=>$cookie_data, -expires=>'+6M'); print $cgiobject->header(-cookie=>$final_cookie); } 1;
but I couldn't get it to run. I tried using SYSTEM and then EXEC, neither of which I've ever tried using before, but to no avail.

Replies are listed 'Best First'.
Re^3: Cookies write to screen, not to cookie file
by antirice (Priest) on Jul 01, 2004 at 21:07 UTC

    Uh...try this?

    common.cgi

    #!/usr/bin/perl #################################### use CGI qw/:standard/; use CGI::Cookie; sub write_cookie { my $c = new CGI::Cookie(-name => 'foo', -value => ['bar','baz'], -expires => '+3M' ); print header(-cookie=>[$c]); %cookies = fetch CGI::Cookie; ## This is very bad. You should check that $cookies{'ID'} exists ## or your script will die when you run this code. # $id = $cookies{'ID'}->value; ## You've already filled %cookies with the cookies. Why ## do it again? # %cookies = parse CGI::Cookie($ENV{COOKIE}); } 1;

    script.pl

    #!/usr/bin/perl require "common.cgi"; write_cookie(); print "Something";

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Removing the two lines changed the output that I was getting with the CGI::Cookie previously. Instead of having the cookie data print out on a blank page, the cookie data now printed out just above the confirmation message, which is exactly what I was getting with my original, non CGI::Cookie code.

      Using the script.pl, the cookie writes to the cookie file as it should, but I don't have the data that I want to write, so it can't be incorporated in the system.

      It appears that I either need to somehow turn off the  content command that sets up the output buffer, or I need to go back to the drawing board and redo the system.

        This suggestion may tick you off, but it's only a suggestion. You may want to eventually migrate to something like CGI::Application. The way it works is that you build up the output in a run_mode and within this runmode you can change things such as what the headers will print when you're ready to go. HTTP is all text and certain things are expected to happen. Once you get beyond certain points (i.e. headers are already sent) then it's too late to send out cookies or anything else for that matter.

        A short-term possibility is to move most of the logic for detecting cookies and whatnot up towards the beginning of the script. Then you just set flags for later use but use them at the beginning while printing out the headers to make certain you send everything you need.

        Of course, I may be reading everything incorrectly and coming across as an idiot. If so, then I apologize as I've been up for an extraordinary amount of time.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1