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

Hi monks,
Im using windows 2k pro, apache, IE
The problem is with page redirect; this is my code “login scripts”;
#Get user ID and password ….. #Validate user…… #Of the user is validated { #create session #create the cookie $cookie = $query->cookie(CGISESSID => $session->id); #send the cookie to the browser print $query->header( -cookie=>$cookie ); #redirct the user to welcome page print $query -> redirect("http://localhost/cgi-bin/welcome.pl"); + }else{ get out

the redirect bit is not working, and if I call the redirect before the cookie header, it works! But the cookie header is ignored and no cookie is sent, so I cannot track the user movement!
I have seen many questions about header() and redirect() and which one needs to be written first! And frankly there aren’t enough details, especially with cookie headers.
So:
1. can I send both the cookie and redirect at the same time?
2. how can I flush out the buffer?
3. is there a way to stop page refreshing initiated by the client?

well they said perl is flexible!

Regards

Replies are listed 'Best First'.
Re: Cookie, redirect
by kwaping (Priest) on Jul 21, 2005 at 21:02 UTC
    I whipped up this little test which both set the cookie and redirected my browser.
    #!/usr/bin/perl use warnings; use strict; use CGI; my $query = new CGI; my $cookie = $query->cookie(CGISESSID => 'werd'); print $query->header( -cookie=>$cookie, -location=>'http://www.google. +com/' ); #<-- this is the important line! exit;
    You should be able to adapt it to your needs.
      thanks for the reply,
      yes, you code doesn't work,
      Im still in login page, with this lines displayed:
      Set-Cookie: CGISESSID=e7ed07c2bae89b1ff8418f42889538b5; path=/ Date: Thu, 21 Jul 2005 21:17:13 GMT Location: http://localhost/cgi-bin/welcome.pl Content-Type: text/html; charset=ISO-8859-1
      is there something wrong with IE?
        Hmm, it worked for me. Are you using CGI.pm? Can you post the code? Maybe there's something else you're overlooking.
Re: Cookie, redirect
by wolf25 (Initiate) on Jul 21, 2005 at 22:29 UTC
    here you are,
    also are running apache server with perl
    #!c:/perl/bin/perl.exe -w $cfh = select (STDOUT); $| = 1; select ($cfh); #the above line is to provide the scripts interpreter location-------- +>scripts: login.pl #---------------------------------------session_start.pl #---------------------------------------#include the libraries: use CGI qw/:standard/; # load standard CGI routines use CGI; #load the object orentird CGI use DBI; # load DBI library use CGI::Session; # load the Session library use CGI::Carp qw(fatalsToBrowser); # load CGI-Carp for browser er +rors handling my $query = new CGI; #---------------------------------------# if( $query->param("Submit") ){ our $message; #-------check the login if ($query->param("login")) { $login1 = $query->param("login"); unless ($login1 =~ /^[a-z0-9_]+$/) { $login = ''; $message .= 'The login ID contains invalid character!<br>'; }else{ $login = $query->param("login") } }else{ my $login = ''; $message .= 'You forget to enter your login ID!<br>'; } #-------check the password if ($query->param("password")) { $password1 = $query->param("password"); unless ($password1 =~ /^[a-z0-9_]+$/) { $password = ''; $message .= 'The password contains invalid character!<br>'; }else{ $password = $query->param("password") } }else{ my $password = ''; $message .= 'You forget to enter your password!<br>'; } if ($login && $password) { #---------------------- my $sqlstatement="bla bla"; my $sth = $dbh->prepare($sqlstatement); $sth->execute || die "Could not execute SQL statement ... maybe invali +d? $DBI::errstr"; my @row=$sth->fetchrow_array; if (@row) { our $first_name = $row[0]; our $last_name = $row[1]; our $user_id = $row[2]; our $admin = $row[3]; our $user_address = $row[4]; #----------create the user session my $session = new CGI::Session("driver:File", $query, {'Directory' => +"C:\\session\\"} ) or die "S!"; $session->header(); my $session_id = $session->id(); #get the session I +D $session->param('username', $username); #add the username +to the session $session->param('user_id', $user_id); #add the user_id t +o the session $session->param('first_name', $first_name); #add the first_nam +e to the session $session->param('last_name', $last_name); #add the last_name + to the session $session->param('address', $user_address); #add the address t +o the session $session->param('admin', $admin); #add the admin ide +ntity to the session #save session params in the $query object $session->expire('+10m'); #session should e +xpire after 10 minutes $cookie = $query->cookie(CGISESSID => $session->id); print $query->header( -cookie=>$cookie ); print $query->header(-type => 'text/html', -cookie=>$cookie, -location +=>'http://localhost/cgi-bin/cars/loggedin.pl' ); exit; #-------------- finish MySQL statement $sth->finish(); #-------------- disconnect from database $dbh->disconnect(); }else{ $message .= 'The username and the password do not match <br>t +hose in the record.<br>'; } }else{ $message .= 'Try again.'; } } #---------the param bracket
Re: Cookie, redirect
by wolf25 (Initiate) on Jul 21, 2005 at 23:09 UTC
    It’s working now! ;->
    U have no idea what u did to me, thanks u vvvv very much man. U r the greats monk
      Glad it's working now! :) I assume you took out the first "print $query->header( -cookie=>$cookie );" to fix it? I think you can also safely remove the "-type => 'text/html'," part from the second one. You're not actually outputting any HTML, just redirecting.
        U r correct, also I took the ‘-type => 'text/html' out, as u said, and now it works as Perl should be,
        hey monk thanks again for this straight forward answer,
        keep up the good job ;)