Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by lauratheperlnewbie (Initiate)
on Aug 19, 2011 at 09:06 UTC ( [id://921186]=perlquestion: print w/replies, xml ) Need Help??

lauratheperlnewbie has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone! Now im very new to perl so here goes. I have a perl generated webpage which you can save a path(typed in by the user) as a cookie. When I press apply cookie, it regenerates the same page with the new cookie saved. However when this is clicked, "Content-Type:text/html" is shown at the top of my webpage which i dont really want. This is generated by

if ($type eq "nme") { $message = lmhandler::getAdd($find,$loadmap); } elsif ($type eq "addr") { $message = lmhandler::getmod($find,$loadmap); } elsif ($type eq "root") #create new cookie { $cookie = $query->cookie(-name=>'MY_COOKIE', -value=> $find, -expires=>'+1y', -path=>'/'); print header( -cookie=>$cookie); }

please could you help me figure out how to stop this? Regards L :)

Replies are listed 'Best First'.
Re: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by charlesboyo (Beadle) on Aug 19, 2011 at 09:16 UTC
    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?

      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

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

        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
Re: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by Anonymous Monk on Aug 19, 2011 at 09:12 UTC
Re: Perl CGI Cookies "Content-Type:text/html" showing at top of html.
by sundialsvc4 (Abbot) on Aug 19, 2011 at 13:54 UTC

    Here is where a client-side debugger such as FireBug is extremely useful.   It really helps to “see for yourself,” byte for byte, what the client is actually receiving.   Sometimes that is a lot more insightful than looking at the code and trying to figure out just what it is sending.   Obviously, at some point the browser decided it wasn’t looking at HTML headers anymore, so LQQK at what it says it got.

      It is worth to note that Firefox does not show in its "View Source" part and in Firebug what it received, but what DOM it constructed from that. Especially if it is receiving "invalid" HTML (according to some standard or other), the DOM might or might not reflect the intention of what was sent.

        No, I am specifically thinking of the “Net” displays of FireBug, which is of course the debugging plug-in for FireFox.

        Other browsers offer similar developer-oriented debugging features.

        I have more than one flat-spot on my head (“Doh!!”) from things that Firebug has told me about what an application was actually sending.   These sorts of problems are usually fairly effortless to resolve once you can see them, but, “aye, there’s the rub.”

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 12:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found