<?xml version="1.0" encoding="windows-1252"?>
<node id="263855" title="CGI + SSI = extra content-type header?" created="2003-06-06 16:44:55" updated="2005-06-16 11:03:05">
<type id="115">
perlquestion</type>
<author id="6040">
Kozz</author>
<data>
<field name="doctext">
Most esteemed monks:
&lt;p&gt;I have been wrestling with a problem in a CGI script which is included in a .html file as &lt;code&gt;#exec cgi="retrieve_password.pl"&lt;/code&gt;.  (".html" has been set to server-parsed html).&lt;/p&gt;
&lt;p&gt;A form (method=GET) calls another html page, resulting in a GET query of &lt;code&gt;"/retrieve_password.html?email=user@domain.com"&lt;/code&gt;.  Then the QUERY_STRING is passed along to retrieve_password.pl through the Apache sub-request.&lt;/p&gt;
&lt;p&gt;However, upon output, the &lt;code&gt;Content-type: text/html&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;It seems that if I comment-out my call to &lt;code&gt;send_account()&lt;/code&gt; that the header no longer becomes visible.  But why is this?&lt;/p&gt;

&lt;p&gt;Any help you can provide would be most welcome.&lt;/p&gt;

&lt;readmore&gt;
&lt;code&gt;
#!/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-&gt;header;

if($q-&gt;param('email') eq ''){
	print $q-&gt;p(qq{&lt;p&gt;Please &lt;a href="javascript:history.go(-1)"&gt;go back&lt;/a&gt; and provide your account email address.&lt;/p&gt;\n});
	exit;
}

my $query = sprintf(q{SELECT email, namefirst, namelast, username, userpass from %s WHERE email ~ %s}, 
		$table, qstr($q-&gt;param('email')) );
my $result = $sql-&gt;execute($query);
my  @data = $result-&gt;fetchrow();


my $email = $data[0];

if($email ne ''){
	send_account(\@data);
	print $q-&gt;p( "Thank you!  Your account information has been sent to " . $email );
	print $q-&gt;p( qq{&lt;a href="/login.php"&gt;Log In&lt;/a&gt;} );
}else{
	print $q-&gt;p(qq{&lt;p&gt;Sorry!  No account found with that email address.  Please } . 
			qq{&lt;a href="javascript:history.go(-1)"&gt;go back&lt;/a&gt; and try again.&lt;/p&gt;\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-&gt;to( shift @{$matching_row} );
	$mailout-&gt;subject("Online Registration");
	$mailout-&gt;add('From', 'user@domain.com');
	$mailout-&gt;add('Reply-To', 'user@domain.com');

	# open up a mail filehandle
	my $mailfh = $mailout-&gt;open;

	# print mail body
	my $mailbody=&lt;&lt;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 first
  order. 

Sincerely,

Administrator
END_OF_MAIL

	printf $mailfh $mailbody, @{$matching_row};

	# close and send
	$mailfh-&gt;close;   
}
&lt;/code&gt;
&lt;/readmore&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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-&gt;header;" line.&lt;/p&gt;
</field>
</data>
</node>
