how to remove duplicate header line in CGI

D

dmedhora

Hi,
When I write a CGI script in perl I seem to get this line printed out
TWICE !

Content-Type: text/html; charset=iso-8859-1

It also shows up in the web browser as the first line, when I execute
the same from my cgi-bin
dir.

Any idea how I may get rid of this duplication ? It could be causing
problems while
I debug and run my programs..

Thanks:)
 
X

xhoster

Hi,
When I write a CGI script in perl I seem to get this line printed out
TWICE !

Content-Type: text/html; charset=iso-8859-1

It also shows up in the web browser as the first line, when I execute
the same from my cgi-bin
dir.

Any idea how I may get rid of this duplication ?

Figure out which part of your code is printing both of these, and take out
whichever part it makes the most sense to take out.

You could try to tie STDOUT to something that will filter out
a second header, but at that point you pretty much have to face
the fact that the code is now in control of you and not the other
way around. Time to start working on a fundamental re-design.

If you use fatalsToBrowser then a fatal error might cause an extra header
to be printed if one has already been printed. This has never caused
me a problem, so I just live with it. It tries to guess if you have
already printed a header, but I believe the way it does this, and
the effectiveness of it, changes from version to version.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
T

Tad McClellan

When I write a CGI script in perl I seem to get this line printed out
TWICE !

Content-Type: text/html; charset=iso-8859-1
Any idea how I may get rid of this duplication ?


Yes. Find the 2 places in your code that are outputting that header,
and remove one of them.
 
B

Ben Bullock

Hi,
When I write a CGI script in perl I seem to get this line printed out
TWICE !

Content-Type: text/html; charset=iso-8859-1

If I try this:

#!/usr/bin/perl
use warnings;use strict;
use CGI;
my $nice_cgi=CGI::new();
print $nice_cgi->header();

I just get it once.
It also shows up in the web browser as the first line, when I execute
the same from my cgi-bin
dir.

Any idea how I may get rid of this duplication ?

It's really, really hard to say why the duplication is occurring without
looking at your script. Perhaps you have something like this, for example:

print $nice_cgi->header();
print $nice_cgi->header();

That would cause the problem you describe. Alternatively,

for (my $i=0; $i < 2; $i++) {
print $nice_cgi->header();
}

would also suffice to create this computational conundrum.
 
D

dmedhora

Hi,
When I write a CGI script in perl I seem to get this line printed out
TWICE !

Content-Type: text/html; charset=iso-8859-1

It also shows up in the web browser as the first line, when I execute
the same from my cgi-bin
dir.

Any idea how I may get rid of this duplication ? It could be causing
problems while
I debug and run my programs..

Thanks:)

Hi and Thanks for your posts, Well, to add more details to this
It happens only when I use CGI::FormBuilder
So, if I use CGI.pm there is no duplication but if I use
CGI::FormBuilder then I get that duplicate.
Here is some code :)
==========================================================
#!/usr/bin/perl

use CGI::FormBuilder;

my @fields = qw(category task status notes);

my $form = CGI::FormBuilder->new(
method => 'post',
fields => \@fields,
validate => {
status => 'INT', # validate
},
required => 'ALL',
);

if ($form->submitted)
{
print $form->confirm(header => 1);
} else {
print $form->render(header => 1);
}
=========================================================

Note, since I got this problem I've dumped FormBuilder and
settled for plain CGI.pm instead.
 
B

Ben Bullock

Hi and Thanks for your posts, Well, to add more details to this
It happens only when I use CGI::FormBuilder
So, if I use CGI.pm there is no duplication but if I use
CGI::FormBuilder then I get that duplicate.

I ran the script you supplied, but I only got the offending message once.
 
D

dmedhora

Same here.

Hi,
Well when I run the script on the command line I get two headers i.e
"Content Type" lines
printed out, is that good or bad ?
I have a cat and head display below:
[root@localhost cgi-bin]# cat h.pl
#!/usr/bin/perl
use CGI::FormBuilder;

my @fields = qw(category task status notes);

my $form = CGI::FormBuilder->new(
method => 'post',
fields => \@fields,
validate => {
status => 'INT', # validate
},
required => 'ALL',
);

if ($form->submitted)
{
print $form->confirm(header => 1);
} else {
print $form->render(header => 1);
}
[root@localhost cgi-bin]# ./h.pl | head
Content-Type: text/html; charset=iso-8859-1

Content-Type: text/html; charset=iso-8859-1

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US"
xml:lang="en_US">

[root@localhost cgi-bin]#
 
B

Ben Bullock

Well when I run the script on the command line I get two headers i.e
"Content Type" lines
printed out, is that good or bad ?

That's bad.
I have a cat and head display below:
[root@localhost cgi-bin]# ./h.pl | head
Content-Type: text/html; charset=iso-8859-1

Content-Type: text/html; charset=iso-8859-1

It might be a bug in your version of CGI::FormBuilder. I'm using the
latest version, including dependencies. One suggestion which might fix
this is you could try using "cpan upgrade" to make sure that you're up to
date with all the modules you're using. Also it might be a good idea to
try running the same script on a different machine.
 
M

Mumia W.

Hi,
Well when I run the script on the command line I get two headers i.e
"Content Type" lines
printed out, is that good or bad ? [...]

As Mr. Morrow said, that's bad.

When I run the following script on the command line, I get only one
Content-Type header:

----------------------script------------------
#!/usr/bin/perl

use CGI::FormBuilder;
use Module::Versions::Report;

my @fields = qw(category task status notes);

my $form = CGI::FormBuilder->new(
method => 'post',
fields => \@fields,
validate => {
status => 'INT', # validate
},
required => 'ALL',
);

if ($form->submitted)
{
print $form->confirm(header => 1);
} else {
print $form->render(header => 1);
}
----------------------end---------------------

Here is the output:

Content-Type: text/html; charset=ISO-8859-1

<html><head><title>Form Build</title>
<script language="JavaScript1.3"><!-- hide from old browsers
function validate (form) {
var alertstr = '';
var invalid = 0;

// standard text, hidden, password, or textarea box
var category = form.elements['category'].value;
if ( ((! category && category != 0) || category === "")) {
alertstr += '- You must enter a valid value for the "Category"
field\n';
invalid++;
}
// standard text, hidden, password, or textarea box
var task = form.elements['task'].value;
if ( ((! task && task != 0) || task === "")) {
alertstr += '- You must enter a valid value for the "Task"
field\n';
invalid++;
}
// standard text, hidden, password, or textarea box
var status = form.elements['status'].value;
if ( (! status.match(/^-?\s*[0-9]+$/)) ) {
alertstr += '- You must enter a valid value for the "Status"
field\n';
invalid++;
}
// standard text, hidden, password, or textarea box
var notes = form.elements['notes'].value;
if ( ((! notes && notes != 0) || notes === "")) {
alertstr += '- You must enter a valid value for the "Notes"
field\n';
invalid++;
}
if (invalid > 0 || alertstr != '') {
if (! invalid) invalid = 'The following'; // catch for
programmer error
alert(''+invalid+' error(s) were encountered with your
submission:'+'\n\n'+alertstr+'\n'+'Please correct these fields and try
again.');
// reset counters
alertstr = '';
invalid = 0;
return false;
}
return true; // all checked ok
}
//-->
</script><noscript><font color="red"><b>Please enable JavaScript or use
a newer browser</b></font></noscript><p></head><body
bgcolor="white"><h3>Form Build</h3><p>Fields shown in <b>bold</b> are
required.
<!-- Generated by CGI::FormBuilder v2.12 available from
www.formbuilder.org -->
<form action="form-build.cgi" method="post" onSubmit="return
validate(this);"><input name="_submitted" type="hidden" value="1"
/><input name="_sessionid" type="hidden" value="" /><table>
<tr valign="middle"><td align="left"><b>Category</b></td><td><input
name="category" type="text" /></td></tr>
<tr valign="middle"><td align="left"><b>Task</b></td><td><input
name="task" type="text" /></td></tr>
<tr valign="middle"><td align="left"><b>Status</b></td><td><input
name="status" type="text" /></td></tr>
<tr valign="middle"><td align="left"><b>Notes</b></td><td><input
name="notes" type="text" /></td></tr>
<tr valign="middle"><td colspan="2"><center><input name="_reset"
type="reset" value="Reset" /><input name="_submit" type="submit"
value="Submit" /></center></td></tr></table>
</form></body></html>


Perl v5.8.4 under linux
Modules in memory:
attributes;
Carp v1.02;
CGI v3.04;
CGI::FormBuilder v2.12;
CGI::Util v1.4;
CGITempFile;
constant v1.04;
DynaLoader;
Exporter v5.58;
Fh;
Internals;
Module::Versions::Report v1.02;
MultipartBuffer;
overload v1.01;
PerlIO;
PerlIO::Layer;
Regexp;
strict v1.03;
UNIVERSAL;
utf8;
vars v1.01;
warnings v1.03;
warnings::register v1.00;
[at Wed Sep 19 12:41:01 2007 (local) / Wed Sep 19 17:41:01 2007 (GMT)]
 
D

dmedhora

Hi,
Well when I run the script on the command line I get two headers i.e
"Content Type" lines
printed out, is that good or bad ? [...]

As Mr. Morrow said, that's bad.

When I run the following script on the command line, I get only one
Content-Type header:

----------------------script------------------
#!/usr/bin/perl

use CGI::FormBuilder;
use Module::Versions::Report;

my @fields = qw(category task status notes);

my $form = CGI::FormBuilder->new(
method => 'post',
fields => \@fields,
validate => {
status => 'INT', # validate
},
required => 'ALL',
);

if ($form->submitted)
{
print $form->confirm(header => 1);} else {

print $form->render(header => 1);}

----------------------end---------------------

Here is the output:

Content-Type: text/html; charset=ISO-8859-1

<html><head><title>Form Build</title>
<script language="JavaScript1.3"><!-- hide from old browsers
function validate (form) {
var alertstr = '';
var invalid = 0;

// standard text, hidden, password, or textarea box
var category = form.elements['category'].value;
if ( ((! category && category != 0) || category === "")) {
alertstr += '- You must enter a valid value for the "Category"
field\n';
invalid++;
}
// standard text, hidden, password, or textarea box
var task = form.elements['task'].value;
if ( ((! task && task != 0) || task === "")) {
alertstr += '- You must enter a valid value for the "Task"
field\n';
invalid++;
}
// standard text, hidden, password, or textarea box
var status = form.elements['status'].value;
if ( (! status.match(/^-?\s*[0-9]+$/)) ) {
alertstr += '- You must enter a valid value for the "Status"
field\n';
invalid++;
}
// standard text, hidden, password, or textarea box
var notes = form.elements['notes'].value;
if ( ((! notes && notes != 0) || notes === "")) {
alertstr += '- You must enter a valid value for the "Notes"
field\n';
invalid++;
}
if (invalid > 0 || alertstr != '') {
if (! invalid) invalid = 'The following'; // catch for
programmer error
alert(''+invalid+' error(s) were encountered with your
submission:'+'\n\n'+alertstr+'\n'+'Please correct these fields and try
again.');
// reset counters
alertstr = '';
invalid = 0;
return false;
}
return true; // all checked ok}

//-->
</script><noscript><font color="red"><b>Please enable JavaScript or use
a newer browser</b></font></noscript><p></head><body
bgcolor="white"><h3>Form Build</h3><p>Fields shown in <b>bold</b> are
required.
<!-- Generated by CGI::FormBuilder v2.12 available fromwww.formbuilder.org-->
<form action="form-build.cgi" method="post" onSubmit="return
validate(this);"><input name="_submitted" type="hidden" value="1"
/><input name="_sessionid" type="hidden" value="" /><table>
<tr valign="middle"><td align="left"><b>Category</b></td><td><input
name="category" type="text" /></td></tr>
<tr valign="middle"><td align="left"><b>Task</b></td><td><input
name="task" type="text" /></td></tr>
<tr valign="middle"><td align="left"><b>Status</b></td><td><input
name="status" type="text" /></td></tr>
<tr valign="middle"><td align="left"><b>Notes</b></td><td><input
name="notes" type="text" /></td></tr>
<tr valign="middle"><td colspan="2"><center><input name="_reset"
type="reset" value="Reset" /><input name="_submit" type="submit"
value="Submit" /></center></td></tr></table>
</form></body></html>

Perl v5.8.4 under linux
Modules in memory:
attributes;
Carp v1.02;
CGI v3.04;
CGI::FormBuilder v2.12;
CGI::Util v1.4;
CGITempFile;
constant v1.04;
DynaLoader;
Exporter v5.58;
Fh;
Internals;
Module::Versions::Report v1.02;
MultipartBuffer;
overload v1.01;
PerlIO;
PerlIO::Layer;
Regexp;
strict v1.03;
UNIVERSAL;
utf8;
vars v1.01;
warnings v1.03;
warnings::register v1.00;
[at Wed Sep 19 12:41:01 2007 (local) / Wed Sep 19 17:41:01 2007 (GMT)]

Guys!, I upgraded to 3.0501 as you suggested and voila
its fine! Damn, did I waste a lotta time on this one,
Thanks for your help. I Wonder why I didn't think an
upgrade would help in the first place...:)
Now, the question is ( and u don't have to answer this :)
Do I go the CGI::FormBuilder route or CGI.pm + Javascript
route ? Any ideas on which one is more "your" choice ?
Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top