Cookies mit Perl lesen !!!
Ansgar
- perl
0 Matti Maekitalo0 RTFM
Hallo,
ich habe eine Cookie erfolgreich geschrieben !!! (super)
Aber WIE kann ich ihn wieder auslesen ?
Warum klappt das folgende nicht ?
-------------------------------------------------------
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Cookie;
my $wert = cookie("nowis");
print "Content-type: text/html\n\n";
print "<html>";
print "Wert: $wert";
print "</html>\n";
-------------------------------------------------------
vielen dank schon mal im voraus für eure hilfe
cu
ansgar
use Mosche;
ich habe eine Cookie erfolgreich geschrieben !!! (super)
Aber WIE kann ich ihn wieder auslesen ?
Ich muss zugeben, daß ich zuvor noch nie mit Cookies gearbeitet habe. Aber mal einen kleinen Blick in perldoc CGI::Cookie [1] geworfen, und siehe da:
# fetch existing cookies
%cookies = fetch CGI::Cookie;
$id = $cookies{'ID'}->value;
# oder
# create cookies returned from an external source
%cookies = parse CGI::Cookie($ENV{COOKIE});
Das sollte funktionieren.
[1] perldoc CGI::Cookie
auf der Kommandozeile oder http://www.perldoc.com
use Tschoe qw(Matti);
Hallo,
ich habe eine Cookie erfolgreich geschrieben !!! (super)
Aber WIE kann ich ihn wieder auslesen ?
Warum klappt das folgende nicht ?
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Cookie;
my $wert = cookie("nowis");
print "Content-type: text/html\n\n";
print "<html>";
print "Wert: $wert";
print "</html>\n";vielen dank schon mal im voraus für eure hilfe
cu
ansgar
RTFM!
http://stein.cshl.org/WWW/software/CGI/#cookies
The interface to HTTP cookies is the cookie() method:
$cookie = $query->cookie(-name=>'sessionID',
-value=>'xyzzy',
-expires=>'+1h',
-path=>'/cgi-bin/database',
-domain=>'.capricorn.org',
-secure=>1);
print $query->header(-cookie=>$cookie);
cookie() creates a new cookie. Its parameters include:
-name
The name of the cookie (required). This can be any string at all. Although Netscape limits its cookie names to non-whitespace alphanumeric characters, CGI.pm removes this restriction by escaping and unescaping cookies behind the scenes.
-value
The value of the cookie. This can be any scalar value, array reference, or even associative array reference. For example, you can store an entire associative array into a cookie this way:
$cookie=$query->cookie(-name=>'family information',
-value=>%childrens_ages);
-path
The optional partial path for which this cookie will be valid, as described above.
-domain
The optional partial domain for which this cookie will be valid, as described above.
-expires
The optional expiration date for this cookie. The format is as described in the section on the header() method:
"+1h" one hour from now
-secure
If set to true, this cookie will only be used within a secure SSL session.
The cookie created by cookie() must be incorporated into the HTTP header within the string returned by the header() method:
print $query->header(-cookie=>$my_cookie);
To create multiple cookies, give header() an array reference:
$cookie1 = $query->cookie(-name=>'riddle_name',
-value=>"The Sphynx's Question");
$cookie2 = $query->cookie(-name=>'answers',
-value=>%answers);
print $query->header(-cookie=>[$cookie1,$cookie2]);
To retrieve a cookie, request it by name by calling cookie() method without the -value parameter:
use CGI;
$query = new CGI;
%answers = $query->cookie('answers');
To retrieve the names of all cookies passed to your script, call cookie() without any parameters. This allows you to iterate through all cookies:
foreach $name ($query->cookie()) {
print $query->cookie($name);
}
The cookie and CGI namespaces are separate. If you have a parameter named 'answers' and a cookie named 'answers', the values retrieved by param() and cookie() are independent of each other. However, it's simple to turn a CGI parameter into a cookie, and vice-versa:
# turn a CGI parameter into a cookie
$c=$q->cookie(-name=>'answers',-value=>[$q->param('answers')]);
# vice-versa
$q->param(-name=>'answers',-value=>[$q->cookie('answers')]);
See the cookie.cgi example script for some ideas on how to use cookies effectively.
NOTE: There are some limitations on cookies. Here is what RFC2109, section 6.3, states:
Practical user agent implementations have limits on the number and
size of cookies that they can store. In general, user agents' cookie
support should have no fixed limits. They should strive to store as
many frequently-used cookies as possible. Furthermore, general-use
user agents should provide each of the following minimum capabilities
individually, although not necessarily simultaneously:
* at least 300 cookies
* at least 4096 bytes per cookie (as measured by the size of the
characters that comprise the cookie non-terminal in the syntax
description of the Set-Cookie header)
* at least 20 cookies per unique host or domain name
User agents created for specific purposes or for limited-capacity
devices should provide at least 20 cookies of 4096 bytes, to ensure
that the user can interact with a session-based origin server.
The information in a Set-Cookie response header must be retained in
its entirety. If for some reason there is inadequate space to store
the cookie, it must be discarded, not truncated.
Applications should use as few and as small cookies as possible, and
they should cope gracefully with the loss of a cookie.
Unfortunately, some browsers appear to have limits that are more restrictive than those given in the RFC. If you need to store a lot of information, it's probably better to create a unique session ID, store it in a cookie, and use the session ID to locate an external file/database saved on the server's side of the connection.