in reply to Cookies, how can you set one if it must be done before the header?

What exactly is your question?

Your pseudocode is kind of illogical.

How about this:

if cookie doesn't exist { if (the form has been submitted){ check form for authenticity set cookie from login form print html headers print rest of script } else { # form not submitted print html headers print login form } } else { print html headers print rest of script }

There are three states, right? Cookie doesn't exist and this is a form submission, cookie doesn't exist and this is a form submission, and cookie does exist.

Does that make sense?



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: Re: Cookies
by Anonymous Monk on Dec 24, 2003 at 10:08 UTC
    Thank you for your help, I've used your code and got pretty far with it, I believe, but I've run into a problem or two. I don't know if the cookie is getting set or not, but it says I'm logged in if I type in the correct password.

    I have url_params that run off this script, but when I'm supposively logged in and I go to a url_param, it errors out saying I have the wrong password. Can you see a problem below with the cookie being sent? And if I reload the page after I log in, I stay logged in. But if I go back to the url using the location bar, it tells me to relog back in.

    Thank you!

    my $adminpassword = "test"; my %cookie; if ( !exists $cookie{'pass'} ) { if ( param() ) { my $adminpass = param('admin'); if ( $adminpass eq $adminpassword ) { my $cookiename = cookie( -name => 'cookie', -value => 'loggedin', -expires => '+1h' ); print header, start_html(); # print rest of page here print "you are logged in"; } } else { print header, start_html(); print "Incorrect password, please click back and try again +"; exit; } } else { print header, start_html(); print start_form(), table( Tr( td("Admin Password: "), td( textfield( -name => 'admin', -size => 10 ) ) ), Tr( td(), td(submit) ), ), end_form(), hr(); } }
      I have found that Mozilla is very useful for tracking the setting and sending of cookies. All you need to do is turn on the 'Ask me before setting cookies' option in the privacy settings and you get an informational dialog each time that a cookie is set. You can then use the cookie manager to inspect the cookies that have been set.

      You may also want to dump the HTTP headers that your app received as HTML comments while you are building and debugging your app. Using Data::Dumper on your cgi object(or other objects) is also useful.

      IE requires the path be set in your cookie..

      my $cookiename = cookie( -name => 'cookie', -value => 'loggedin', -expires => '+1h' -path => '/', );

      cheers,

      J

        I added the path but it still doesn't change the fact that I can log in, but if I go back to the url 2 seconds afterwards it forces me to log in again.