Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 ( [id://921196]=note: print w/replies, xml ) Need Help??


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

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 :)
  • Comment on Re^2: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
  • Download Code

Replies are listed 'Best First'.
Re^3: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by wfsp (Abbot) on Aug 19, 2011 at 09:41 UTC
    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)
Re^3: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by Anonymous Monk on Aug 19, 2011 at 09:39 UTC

    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?

        I must print the cookie into the h....and for the html to work correctly I need

        Kindly go back to the manuals I linked :)

        You can only print the header once, and once only, this is the rule of HTTP, no way around it

        Instead of broken logic

        if ( blah ){ print header( and cookie ); } print header;
        use correct logic for print headers only once
        if ( blah ){ store cookie in variable } print header ( and $cookie )
Re^3: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by charlesboyo (Beadle) on Aug 19, 2011 at 09:44 UTC
    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
Re^3: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by lauratheperlnewbie (Initiate) on Aug 22, 2011 at 09:58 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://921196]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found