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

I'm doing an application with CGI, CGI::Application and HTML::Template and need to set some cookies.

The only method i can get to set the cookie is: print  q->header(-cookie=>$cookie)

While this works, it also prints up on the screen the Content Type stuff ( Content-Type: text/html; charset=ISO-8859-1 ) i'd prefer it not to display. After reading the documentation it mentions i need to muck around with the HTML header, but for the life of me I cant work it out.

If someone could give me an example, or point me to some more verbose documentation, i'd gladly appreciate it.

Replies are listed 'Best First'.
Re: HTML::Template and setting Cookies
by rob_au (Abbot) on Dec 08, 2001 at 15:31 UTC
    Your problems in this setting are with the CGI::Application framework rather than with HTML::Template - With CGI::Application, you cannot print any of the headers or page content directly to the client. The way by which you can set page headers is through the header_props method exported from the CGI::Application module - All header parameters passed through this module are passed onto the CGI header function.

    A sample piece of code from a CGI::Application run-mode subroutine which makes use of this method to set a cookie might look like this:

    sub user_login { my $self = shift; my $cgi = $self->query; my $xml = $self->param('xml'); $self->header_props( -uri => '/' ); if ((defined $cgi->param('username')) && (defined $cgi->param('pas +sword'))) { my @user = grep { $_->{'username'} eq $cgi->param('username') +} @{%{$xml}->{'user'}}; if (scalar @user) { my $user = shift @user; if ($user->{password} eq md5_hex($cgi->param('password'))) + { $self->header_props( -cookie => $cgi->cookie( -expires => '+1y', -name => 'cowsnet-auth', -path => '/', -value => \%{$user} ), -uri => '/' ); } } }; $self->header_type('redirect'); }

    In this code the Set-Cookie header is set through the -cookie argument which is passed onto CGI - Additionally a Location header is set the header_type is set to redirect the user to the URL specified in the Location header.

    In addition to the CGI::Application documentation, you might want to look at the module review on this module here.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: HTML::Template and setting Cookies
by rogueFalcon (Beadle) on Dec 09, 2001 at 03:38 UTC
    Here is another example if this helps you... I had the same problem. CGI::App rules when you know how to use it but it is a love hate relationship :-)

    sub Login
    {
        # Local variables
        my $self = shift;
        my $cgi = $self->query();
        my $permissions = "";
        my $template;
    	
        # Check the database for the username and password
        my $dbh = DBI->connect("DBI:mysql:$databaseName", $dbUser, $dbPass) or die "Could not connect to database: " . DBI->errstr;
        my $sth = $dbh->prepare('SELECT Users.permissions FROM Users WHERE username=? AND password=?');
        $sth->execute($cgi->param('username'), $cgi->param('password')) or die DBI->errstr;
    	
        # see if the user was in the database
        if (my @permissions = $sth->fetchrow_array)
        {
            # They have a valid login. Get the template
            $template = $self->load_tmpl("loginVALID.html");
    		
            # Make a cookie
            my $cookie = $cgi->cookie(-name => 'browserID',
                                      -value => '55',
                                      -expires => '+30m',
                                      -domain => 'dshack.com');
    							  
    	# Give the cookie to the browser		
            $self->header_props(-cookie => $cookie);								  
    	# Set the redirect
            $template->param('redirect',cgi->param("redirect"));
        }
        else
        {
    	# they do not have a valid login
    	$template = $self->load_tmpl("login.html");
    
            # Display the Bad username and password warning	
            $template->param('INVALID', '1');
        }
    
        return $template->output;	
    }
    
    Hope this helps. -- RogueFalcon
    Why do you people insist on doing things sdrawkcab?
Re: HTML::Template and setting Cookies
by Ryszard (Priest) on Dec 09, 2001 at 12:29 UTC

    Hey cool, thanks both for your examples. Funnily enuff, both examples are pretty damn close to what i'm doing (ie authentication).

    It's all sorted now and the page in question looks a whole lot cleaner.. ;-). I'm off to read some more documentation...