Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

CGI + SSI = extra content-type header?

by Kozz (Friar)
on Jun 06, 2003 at 20:44 UTC ( #263855=perlquestion: print w/ replies, xml ) Need Help??
Kozz has asked for the wisdom of the Perl Monks concerning the following question:

Most esteemed monks:

I have been wrestling with a problem in a CGI script which is included in a .html file as #exec cgi="retrieve_password.pl". (".html" has been set to server-parsed html).

A form (method=GET) calls another html page, resulting in a GET query of "/retrieve_password.html?email=user@domain.com". Then the QUERY_STRING is passed along to retrieve_password.pl through the Apache sub-request.

However, upon output, the Content-type: text/html is visible on the webpage! I triple-checked, and I do not see my script outputting headers more than once. But if I edit the script and comment-out the line which prints the header, it produces an error.

It seems that if I comment-out my call to send_account() that the header no longer becomes visible. But why is this?

Any help you can provide would be most welcome.

#!/usr/bin/perl # database names, emails, subjects have been changed # to protect the innocent use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use Postgres; # old module, I know use Mail::Send; my $sql = db_connect("mydbname"); if (!$sql){ die "Unable to open database: $!\n"; } my $table = 'users'; my $q = new CGI; print $q->header; if($q->param('email') eq ''){ print $q->p(qq{<p>Please <a href="javascript:history.go(-1)">go ba +ck</a> and provide your account email address.</p>\n}); exit; } my $query = sprintf(q{SELECT email, namefirst, namelast, username, use +rpass from %s WHERE email ~ %s}, $table, qstr($q->param('email')) ); my $result = $sql->execute($query); my @data = $result->fetchrow(); my $email = $data[0]; if($email ne ''){ send_account(\@data); print $q->p( "Thank you! Your account information has been sent t +o " . $email ); print $q->p( qq{<a href="/login.php">Log In</a>} ); }else{ print $q->p(qq{<p>Sorry! No account found with that email address +. Please } . qq{<a href="javascript:history.go(-1)">go back</a> and try + again.</p>\n}); } sub qstr{ # this old Postgres module does not have its own quote() method. # this is a weak implementation, but better than nothing. (I think +) my ($str) = @_; $str =~ s#(\\)#/#sg; # replace backslashes with slashes $str =~ s/(['])/\\$1/; # escape single-quotes return qq{'$str'}; } sub send_account{ my ($matching_row) = @_; # Set Mail Headers my $mailout = new Mail::Send; $mailout->to( shift @{$matching_row} ); $mailout->subject("Online Registration"); $mailout->add('From', 'user@domain.com'); $mailout->add('Reply-To', 'user@domain.com'); # open up a mail filehandle my $mailfh = $mailout->open; # print mail body my $mailbody=<<END_OF_MAIL; Dear %s %s: Thank you for registering. Here is your account information: USERNAME: %s PASSWORD: %s Please keep this information safe. Your account is active, and you may log in any time to place your fi +rst order. Sincerely, Administrator END_OF_MAIL printf $mailfh $mailbody, @{$matching_row}; # close and send $mailfh->close; }

I realize that some of the code is not the most elegant, and you are welcome to provide corrections, but my most pressing need is this spurious display of the content-type header.

Oh, it may also be worth noting that there are other perl scripts included in the page in a similar fashion, and these do NOT show a visible content-type header, though they DO have a "print $q->header;" line.

Comment on CGI + SSI = extra content-type header?
Select or Download Code
Re: CGI + SSI = extra content-type header?
by waswas-fng (Curate) on Jun 06, 2003 at 20:59 UTC
    Isnt the server responsible for setting content type when parsing html like this?

    -Waswas
      Well, if I omit the header line, it produces an error... However, if I leave the print header line in place and comment-out my send_account() line, it shows the rest of the output correctly but does not make a visible content-type header. See my confusion?
Re: CGI + SSI = extra content-type header?
by benn (Priest) on Jun 07, 2003 at 14:07 UTC
    I think there must be more going on here - maybe something to do with the other scripts, or your SSI setup. As stated above, if the request was made to "my_html_form_with_execs.html", then Apache should spit out the right headers along with the parsed html, which will include *any* output from your script(s).

    None of your scripts should need to print a header - I'm puzzled as to why the other scripts *don't* show the header, if they all have a "print $q->header" line. Have you looked at the source of the HTML to work out exactly what is printing what where and when?

    Again, commenting out the "send_account()" line should make no difference, unless there's an error there somewhere - what do the logs say when you comment out the "print $q->header" line? Is it a "no headers" error or something to do with send_account()?

    Personally, I'd drop the "exec-cgi-inside-server-parsed html" approach and use HTML::Template instead :)

    Cheers,Ben.

Re: CGI + SSI = extra content-type header?
by Anonymous Monk on Jun 07, 2003 at 21:10 UTC
    If Mail::Send forks a new process to send the mail and your output buffer is not empty then whatever is in the output buffer will be printed twice. A $| = 1; should fix the problem if it is indeed the problem I described.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://263855]
Approved by jlk
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2014-09-21 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls