how to remove duplicate header line in CGI

Discussion in 'Perl Misc' started by dmedhora@gmail.com, Sep 17, 2007.

  1. Guest

    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:)
     
    , Sep 17, 2007
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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.
     
    , Sep 17, 2007
    #2
    1. Advertising

  3. <> wrote:

    > 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.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Sep 18, 2007
    #3
  4. Ben Bullock Guest

    On Mon, 17 Sep 2007 13:25:11 -0700, dmedhora wrote:

    > 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.

    > It could be causing
    > problems while
    > I debug and run my programs..
     
    Ben Bullock, Sep 18, 2007
    #4
  5. Guest

    On Sep 17, 9:25 pm, wrote:
    > 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.
     
    , Sep 18, 2007
    #5
  6. Ben Bullock Guest

    On Tue, 18 Sep 2007 06:24:37 -0700, dmedhora wrote:

    > 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.
     
    Ben Bullock, Sep 18, 2007
    #6
  7. Mumia W. Guest

    On 09/18/2007 08:40 AM, Ben Bullock wrote:
    > On Tue, 18 Sep 2007 06:24:37 -0700, dmedhora wrote:
    >
    >> 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.


    Same here.
     
    Mumia W., Sep 18, 2007
    #7
  8. Guest

    On Sep 18, 4:41 pm, "Mumia W." <paduille.4061.mumia.w
    > wrote:
    > On 09/18/2007 08:40 AM, Ben Bullock wrote:
    >
    > > On Tue, 18 Sep 2007 06:24:37 -0700, dmedhora wrote:

    >
    > >> 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.

    >
    > 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]#
     
    , Sep 19, 2007
    #8
  9. Ben Bullock Guest

    On Wed, 19 Sep 2007 06:46:05 -0700, dmedhora wrote:


    > 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.
     
    Ben Bullock, Sep 19, 2007
    #9
  10. Mumia W. Guest

    On 09/19/2007 08:46 AM, wrote:
    >
    > 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)]
     
    Mumia W., Sep 19, 2007
    #10
  11. Guest

    On Sep 19, 6:50 pm, "Mumia W." <paduille.4061.mumia.w
    > wrote:
    > On 09/19/2007 08:46 AM, wrote:
    >
    >
    >
    > > 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!
     
    , Sep 21, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    685
  2. Shailan
    Replies:
    2
    Views:
    1,116
    Shailan
    Dec 15, 2003
  3. kaushikshome
    Replies:
    4
    Views:
    806
    kaushikshome
    Sep 10, 2006
  4. RyanL
    Replies:
    5
    Views:
    339
    Marc 'BlackJack' Rintsch
    Oct 12, 2007
  5. mlt
    Replies:
    2
    Views:
    892
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page