Leser: 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package cookies; use CGI qw/:standard/; my %cookies = (); sub start { foreach ( cookie() ) { my %hash = cookie($_); $cookies{$_} = \%hash; } 1; } sub get { my($name) = @_; return $name ? $cookies{$name} : \%cookies; } 1;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
use strict; use warnings; package cookies; use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Useqq = 1; use CGI qw/:standard/; my %cookies = (); sub start { foreach ( cookie() ) { my %hash = cookie($_); print '### Debug ### Cookie: ', Dumper(%hash), '###### '; $cookies{$_} = \%hash; } 1; } sub get { my($name) = @_; return $name ? $cookies{$name} : \%cookies; } 1; package main; use CGI qw/:standard/; use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Useqq = 1; sub say { print "@_", "\n"; } $ENV{HTTP_COOKIE} = 'url=%2Fcms%2Fadmin%2F12%2F; user=test'; cookies::start; my $c = cookies::get(); say Dumper $c; my $z = cookies::get('url'); say Dumper $z; say 'URL ist ', $z->{'url'}; 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Odd number of elements in hash assignment at C:\TEST\test.pl line 13.
Odd number of elements in hash assignment at C:\TEST\test.pl line 13.
Use of uninitialized value in join or string at C:\TEST\test.pl line 39.
### Debug ###
Cookie:
$VAR1 = "test";
$VAR2 = undef;
######
### Debug ###
Cookie:
$VAR1 = "/cms/admin/12/";
$VAR2 = undef;
######
$VAR1 = {
"url" => {
"/cms/admin/12/" => undef
},
"user" => {
"test" => undef
}
};
$VAR1 = {
"/cms/admin/12/" => undef
};
URL ist
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new; my %data = ('url' => '/some/page.html'); my $cookie = $q->cookie('url' => \%data); print $cookie->as_string;
url=url&%2Fsome%2Fpage.html; path=/kristian
my %hash = cookie($_);
url=%2Fcms%2Fpage.html; path=/
2010-10-27T15:06:07 GwenDragonDanke.
Ich habe vorhin auch rausbekommen, dass im Modul cookies die Zuweisung
Code: (dl )my %hash = cookie($_);
nur bei Cookies mit mehreren Wertepaaren klappt.
Es werden aber wohl auch gültige Cookies der Form
verwendet.Code: (dl )url=%2Fcms%2Fpage.html; path=/
Nur wie überprüfe ich das jetzt, ob ein Cookie mehrere Wertepaare oder nur einen hat?
my @data = cookie($name)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
use strict; use warnings; package cookies; use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Useqq = 1; use CGI qw/:standard/; use CGI::Cookie; my %cookies = (); sub start { foreach ( cookie() ) { my %h = cookie($_); unless (exists $h{$_} ) { my (@h) = %h; @h = reverse @h; %h = ($_,$h[1]); } print '### Debug ### Cookie %h: ', Dumper(\%h), '###### '; $cookies{$_} = \%h; } 1; } sub get { my($name) = @_; return $name ? $cookies{$name} : \%cookies; } 1; package main; use CGI qw/:standard/; use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Useqq = 1; sub say { print "@_", "\n"; } my $cook1 = 'url=url&%2Fcms%2Fadmin%2Fx.cgi&test&77'; say $ENV{HTTP_COOKIE} = $cook1; cookies::start; say Dumper %cookies; my $c = cookies::get('url'); say $c->{'url'}; say $c->{'test'}; say; say '============='; $cook1 = 'url=%2Fcms%2Fadmin%2Fx.cgi'; say $ENV{HTTP_COOKIE} = $cook1; cookies::start; say Dumper %cookies; my $c = cookies::get('url'); say $c->{'url'}; say $c->{'test'}; say; 1;