Re: CGI, Cookie & Header



Heiko Rompel schrieb:
Moin,

auch am Heiligenabend (bzw. -morgen)
bin ich wieder hier.

In einem CGI gibt man ja als erstes einen http-header aus
z.B. mit print "Content-type: text/html\n\n";

Wenn ich dann an einer Stelle im Script einen Cookie
setzen will, darf aber ja noch kein Header aktiv sein.

Also habe ich ein print "</body></html>\n";
vor dem Erzeugen des Cookies mit
   print $q->header(-cookie=>$cookie);   gesetzt.

Aber egal was ich auch anstelle,
der erste Header ist immer aktiv und das Cookie mir nur im Browser angezeigt und nicht
erstellt.
Wie erzwinge ich das schliessen des HTML-Dokuments?

Hi, und frohe Weihnachten!

Die header()-Funktion darfst Du nur einmal aufrufen.
Die HTTP-Antwort besteht ja aus dem Header-Teil,
einer Leerzeile und dem/den Body-Teil(en). header() gibt
auch die Leerzeile zurück.

Also mußt Du _entweder_ die Programmlogik so strukturieren,
daß beim Aufruf von header() alle Infos vorliegen, die
Du brauchst, und dann
print $q->header( -type => "text/html", -cookie=>$cookie, ... );
aufrufen kannst, _oder_ du sammelst erstmal alle Argumente
in einem Array und alle Body-Inhalte in einem Skalar und gibst
beides am Ende des Scripts aus, etwa wie

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

use strict;
use warnings;
use CGI;

my $q = new CGI;

my @header_args = ( -type => "text/html" );
	# irgendwas ...
my $body = "<HTML>\n<HEAD>\n<TITLE>irgendwas</TITLE>\n</HEAD>\n";
	# irgendwas ...
$body .= "<h1>Nur eine Ueberschrift</h1>\n";
	# jetzt steht fest, was in das Cookie reinmuß
my $cookie = "lalalalololo";
push @header_args, ( -cookie => $cookie );

$body .= "</BODY>\n</HTML>\n";

print $q->header( @header_args );
print $body;
----------------------------------------------------------------

Vor dem Aufruf von header() darf _keine_ Ausgabe jedweder Art
erfolgt sein!

HTH
-Christian
.



Relevant Pages

  • Re: how do you set a cookie for axshdocvw
    ... I tried to put the cookie string in the header parameter of navigate. ... The tcptrace tool shows that the cookie part of the header is being ... > sqlAuthCookie to the WebBrowser control. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: how do you set a cookie for axshdocvw
    ... "Teo Lachev" wrote in message ... > WebBrowser control. ... >> I tried to put the cookie string in the header parameter of navigate. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: CSS switcher in Ruby?
    ... The trick is to not change the URL for each style, rather change something else like a cookie or a session variable. ... I'm going to assume that you have figured out some way to set the cookie and have the css load itself. ... When you get a request to dynamic_css.rb and your STYLE cookie is set to to Style1, ... and include the ETag header of the CSS you want to be used. ...
    (comp.lang.ruby)
  • Re: Strange difference when cookie disappears from $_REQUEST in HTTP vs. WAP
    ... Header has been generated to destroy your login session cookie, ...
    (comp.lang.php)
  • Re: IIS Authentication
    ... whatever) continues to send the same authorization data for every subsequent ... I was assuming that the server sends a cookie to maintain the session. ... But I don't see Set:Cookie in response header or Cookie in request header. ... Digest Authentication) is present. ...
    (microsoft.public.inetserver.iis.security)