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

I run this code and it all seems to work other than when the password is correct it will not create the cookie nor will it redirect you to the page. all that it outputs is Set-Cookie: username=param%28%27name%27%29; domain=.storm.prohosting; path=/; expires=Thu, 14-Jun-2001 02:48:01 GMT Date: Thu, 14 Jun 2001 01:48:01 GMT Content-Type: text/html; charset=ISO-8859-1 Location: http://storm.prohosting.com/fightior/cgi-bin/warrior.pl
!/usr/local/bin/perl use CGI ':standard'; print header(); print start_html(); $name=param('name'); open FILE, "user/$name" or die "Error: $!\n"; my %data = (); while ( my $line = <FILE> ) { chomp $line; my ($key, $value) = split(/\s*:\s*/, $line); $data{$key} = $value; } close FILE; #this section checks if the password is correct and lets you into the +game or tells #you that the password was incorect if (param('password') eq $data{password}) {# if the password is correc +t you enter the game my $query = CGI->new(); my $cookie = $query->cookie(-name=>'username', -values=>'param('name')', -expires=>'+1h', -domain=>'.perlmonks.org'); print $query->header(-cookie=>$cookie); print URI: http://www.storm.prohosting.com/cgi-bin/warropr.pl; } if (param('password') ne $data{password}) {#this part tells you if th +e password was not open (NOPASS, "../password.htm"); for $line (<NOPASS>) { print $line; } } close NEWFILE ; close NOPASS; end_html();

Replies are listed 'Best First'.
(Ovid) Re: redirect and cookie problems
by Ovid (Cardinal) on Jun 14, 2001 at 08:33 UTC

    Print the cookie with the redirection and don't print any other headers.

    print $query->redirect( -uri => 'http://www.perlmonks.org/', -cookie => $cookie );

    Note: this doesn't work on IIS 3, 4, or 5 due to a bug. You'll have to add "-nph => 1" to the redirection to get it to work (i.e. use NPH scripts).

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: redirect and cookie problems
by Masem (Monsignor) on Jun 14, 2001 at 05:56 UTC
    Cookies *must* be sent in the header; reprinting CGI->header() with the cookie will do nothing. So you need to delay printing "cgi->header()" until you have your cookie information.

    If you want to redirect, on the other hand, you do not want to print header(), but use the CGI redirect() function.

    With the code you have above, you must rethink your logic on how you authenicate and send the user on. You won't easily modify this code to do that.

    (I'm guessing that merlyn has a column on what you want to do... :-)


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      thank you, but were in the code should i put this "CGI->header()"? i do not see that in my program at all you make it sound like I do. also do I have to add anything to the () portion of it to make the cookie work. Also wht goes inside the () portion of redirect? is it just the address of the page you want to direct them to.
        Well, you need to use "$cgi->header(-cookie=>cookie_stuff)" as the first thing that you print out to get the cookies to be set.

        Redirect just needs the URL as the call.

        Look at the docs for CGI.pm for more details.


        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        For redirection, you may want to read up on this: Generating a Redirection Header.

        Update: I think I was wrong there, I apologize.

        The redirect() function redirects the browser to a different URL. If you use redirection like this, you should not print out a header as well. As of version 2.0, we produce both the unofficial Location: header and the official URI: header. This should satisfy most servers and browsers - from CGI documentation

        Update 2: Depending on what you want to do - I most likely misunderstood. If you want to completely redirect the user - then these nodes will help. CGI and redirect and Trying to redirect ... - I apologize for not pointing to this earlier.. my brain isn't/wasn't working. You want to use Location - to redirect the user to a new site. But, if you just want to inform the user of the new location - then use URI: header - I believe you used that correctly:
        print URI: http://www.storm.prohosting.com/cgi-bin/warropr.pl;

Re: redirect and cookie problems
by Anonymous Monk on Jun 14, 2001 at 06:32 UTC
    I used your sugestions and changed the code. It still does not work now the prgram out puts

    HTTP/1.1 302 Moved Server: Apache/1.3.19 (Unix) Status: 302 Moved Date: Thu, 14 Jun 2001 02:23:04 GMT location: http://storm.prohosting.com/fightior/cgi-bin/warrior.pl

    #!/usr/local/bin/perl use CGI ':standard'; my $query = CGI->new(); my $cookie = $query->cookie(-name=>'username', -values=>"param('name')", -expires=>'+1h', -domain=>'.storm.prohosting'); print $query->header(-cookie=>$cookie); print start_html(); $name=param('name'); open FILE, "user/$name" or die "Error: $!\n"; my %data = (); while ( my $line = <FILE> ) { chomp $line; my ($key, $value) = split(/\s*:\s*/, $line); $data{$key} = $value; } close FILE; #this section checks if the password is correct and lets you into the +game or tells #you that the password was incorect if (param('password') eq $data{password}) {# if the password is correc +t you enter the game print $query->redirect(-uri=>'http://storm.prohosting.com/fightior/cgi +-bin/warrior.pl', -nph=>1); } if (param('password') ne $data{password}) {#this part tells you if th +e password was not open (NOPASS, "../password.htm"); for $line (<NOPASS>) { print $line; } } close NEWFILE ; close NOPASS; end_html();
Re: redirect and cookie problems
by Anonymous Monk on Jun 14, 2001 at 23:31 UTC
    I am about to give up but I cant since I need to complete this and a few more pages by monday. So will the code below create a cookie. I have it at the top of my program. It does not gernate any errors but I I do not think it created a cookie. I use find on my couputer to look fro a file name 'username' and it comes up with nothing.
    use CGI ':standard'; my $query = CGI->new(); my $cookie = $query->cookie(-name=>'username', -values=>"param('name')", -expires=>'+1h', -domain=>'.storm.prohosting'); print $query->header(-cookie=>$cookie);
      The domain needs to be the fully qualified or at least the last n-1 parts of the domain name. ".storm.prohosting" is going to fail since there's no TLD associated with it. Presumable, if your server is named "storm.prohosting.com", you want either exactly that or "prohosting.com".

      Cookies are generally saved by the domain name, not by the "name" that you give it, else you run into huge namespace problems. So you want to look for something that has the domain in it, rather than "username".

      Also, I don't know what you are trying to do with the value line. You probably want -value=>$query->param('name'), if you want to set the value of the cookie to whatever the name was passed as in your code.


      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain