in reply to How do they expect cookies to work (like this)??

After the form is submitted I assume you are showing the browser some kind of success page, correct? You would set the cookies in the headers of _that_ page.


We're not surrounded, we're in a target-rich environment!
  • Comment on Re: How do they expect cookies to work (like this)??

Replies are listed 'Best First'.
Re: Re: How do they expect cookies to work (like this)??
by Anonymous Monk on Sep 30, 2003 at 17:40 UTC
    No. That still doesn't help me SET a cookie after the headers. You just can't call a cookie without setting one and you can't set one after the headers, so how in the world am I supposed to accomplish that? This form has the action ''; meaning it doesn't bring you to a different page, instead it just reloads the one with the form. But with my if/else statements there should be a way to make it all work.

    Can someone show me an example on how to setup a snippet like this?

      Sure, I'll bite:

      #!/usr/bin/perl -w use strict; use CGI qw(header cookie); if (cookie("val")) { print header(),"Your cookie is still set"; } elsif (param("val")) { print header(-cookie=>cookie( -name =>'val', -value=>param("val"), -expires=>'+1M')),"Success! Your cookie should be set"; } else { print header(),q(<html><body><form method="POST" action=""><input ty +pe="text" name="val"><input type="submit" value="Click here"></form>< +/body></html>); }

      Steps: check for the cookie. If present, display still set page. If not, check for a parameter. If present, set the cookie and display success page. If not, display form. I haven't tested the above code, but you should be able to understand what the process entails. I used CGI for all the nasty work. Please click that link and read EVERYTHING. Then check out Ovid's CGI Programming with Perl course.

      Hope this helps.

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

        Thanks antirice, I am sure that's going to work. But I have one more question. What is the elsif doing? I know it's checking for a parameter so do forms (if their action is '') still hace access to everything the user did on the form? I mean, when it redirects back to itself I'll be able to use and play with all the parameters the user used before the page refreshed?

        Thanks!

      Which part is confusing you? You have to wait to send the headers until after you add the cookie to them. Look at this example from the CGI.pm documentation.

      I believe you're confusing the idea of the script with the concept of page as it applies to the web browser.

      The script generates a page with the form, the form is submitted by the user back to the script which handles the submitted params and generates a page with either success or failure. It is in that "page"'s headers that you set your cookie.

      You basically had the logic right AFAICT in your original post's pseudo-code.

      [Jon]