in reply to Perl CGI Cookies "Content-Type:text/html" showing at top of html.

The culprit is the line  print header( -cookie=>$cookie); That prints the HTTP headers, including the cookie.

But it appears your code has already sent the HTTP header, hence this one ends up in the document.

Can you attach more of your code, showing what happens previous to this snippet?

  • Comment on Re: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by lauratheperlnewbie (Initiate) on Aug 19, 2011 at 09:27 UTC
    its probabaly be just being stupid but where have I printed this twice? here is more of my code although shortened.
    #!/usr/bin/perl use CGI qw(:standard); use URI::Escape; use CGI; use CGI::Cookie; use lmhandler; use strict; my $cookie; my $loadmap = "/data/vdrive/Loadmaps/SmallLoadMap.txt"; my $message = ""; my $type; my $buffer = $ENV{'QUERY_STRING'}; my($type,$find) = split(/=/,$buffer); my $query = new CGI; my $theCookie = $query->cookie('MY_COOKIE'); my $encode = uri_unescape($theCookie); if ($encode) { $loadmap = $encode; } else { $loadmap = "/data/vdrive/Loadmaps/SmallLoadMap.txt" } if ($type eq "nme") #check the par +ameter (determines sub to be used) { $message = lmhandler::getAdd($find,$loadmap); } elsif ($type eq "addr") { $message = lmhandler::getmod($find,$loadmap); } elsif ($type eq "root") #create new coo +kie { $cookie = $query->cookie(-name=>'MY_COOKIE', -value=> $find, -expires=>'+1y', -path=>'/'); print header(-cookie=>$cookie); } print "Content-type:text/html\n\n"; print "<html>\n"; print "<head>\n"; print "<META HTTP-EQUIV=Pragma CONTENT=no-cache>\n"; print "<META HTTP-EQUIV=Expires CONTENT=-1>\n"; print "</head>\n"; print "<title>LoadMap Browser</title>\n"; #Start body #search through chosen loadmap print "<br />\n"; print "<form method=GET action=loadmapbrowser.pl>\n"; print "<h2> &nbsp&nbspLoadMap &nbsp&nbsp&nbsp&nbsp <textarea name=root + cols=80 rows=1/>"; print "$loadmap"; print "</textarea> &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <input type=submi +t value=Apply> </FORM>\n"; print "<br />\n"; print "<br />\n"; #end body print "</html>\n";
    Thanksss :)
      print "Content-type:text/html\n\n";
      You will need to add a print header; line in your other if clauses. Otherwise your script will crash. (or set a flag when you have sent it and test the flag to see if it needs sending)

      one print header(-cookie=>$cookie);

      two print "Content-type:text/html\n\n";

      Also, use CGI, don't mess with QUERY_STRING on your own

        Ahh yep I see why it is happening, However to set my cookie correctly I must print the cookie into the header(or so ive read); print header(-cookie=>$cookie); and for the html to work correctly I need to print  print "Content-type:text/html\n\n"; so how do I fix this?
      if ($type eq "nme") #check the par +ameter (determines sub to be used) { $message = lmhandler::getAdd($find,$loadmap); } elsif ($type eq "addr") { $message = lmhandler::getmod($find,$loadmap); } elsif ($type eq "root") #create new coo +kie { $cookie = $query->cookie(-name=>'MY_COOKIE', -value=> $find, -expires=>'+1y', -path=>'/'); print header(-cookie=>$cookie); <-- Printing headers here ... } print "Content-type:text/html\n\n"; <--- and here. print "<html>\n"; print "<head>\n"; print "<META HTTP-EQUIV=Pragma CONTENT=no-cache>\n"; print "<META HTTP-EQUIV=Expires CONTENT=-1>\n"; print "</head>\n"; print "<title>LoadMap Browser</title>\n";

      You'll have to restructure your code a bit. Printing the 'Content-type' header after calling print header(-cookie=>$cookie) counts as twice.

      I'd suggest the following

      if ($type eq "nme") #check the par +ameter (determines sub to be used) { $message = lmhandler::getAdd($find,$loadmap); print "Content-type:text/html\n\n"; } elsif ($type eq "addr") { $message = lmhandler::getmod($find,$loadmap); print "Content-type:text/html\n\n"; } elsif ($type eq "root") #create new coo +kie { $cookie = $query->cookie(-name=>'MY_COOKIE', -value=> $find, -expires=>'+1y', -path=>'/'); print header(-cookie=>$cookie); } print "<html>\n"; ...

        Thank you for all you suggestions and I have tried your solutions but there is something always printed in the header whether it is the content type or the cookie. I cant figure it out for the life of me! Thanks again Laura
      I have fixed this after all your help thanks alot people !!
      print "Content-type: text/html\n"; print header(-cookie=>$cookie); print "\n";
      Who would have known it would be so simple! Thanks L

        I have fixed this after all your help thanks alot people !!

        print "Content-type: text/html\n"; print header(-cookie=>$cookie); print "\n";
        Who would have known it would be so simple! Thanks L

        Its actually much simpler

        use CGI qw/ :standard /; my @futz = ( -cookie=> cookie(1..9), ); print header( @futz ); __END__ Set-Cookie: 1=2; domain=4; path=3; expires=Thu, 01-Jan-1970 00:00:06 G +MT; secure; HttpOnly Date: Mon, 22 Aug 2011 10:18:59 GMT Content-Type: text/html; charset=ISO-8859-1