当我尝试进入"如果存在cookie,则重定向"功能它不会进入。
#!/usr/bin/perl -I/srv/www/abc/m/www
require "api";
my $db = db();
use CGI;
use DBI;
use strict;
use warnings;
#use CGI qw( :standard -nph );
# read the CGI params
my $cgi = CGI->new;
my $cookie = $cgi->param("loggedin");
#my $cookie = $cgi->cookie("loggedin");
if ( $cookie ) {
print $cgi->redirect("http://host.com");
exit;
}
...
if ( $userID ) {
$cgi->cookie( -name => 'loggedin', -value => '1' ,-expires => '+4h', -path => '/' );
print $cgi->redirect("http://host.com");
exit;
}
我想确定我在页面底部设置的Cookie是否存在,如果存在,则执行重定向。此代码位于if ( $cookie )
块内。
答案 0 :(得分:6)
阅读here以了解如何设置和获取 Cookie。基本上是:
-value
创建Cookie。必须将其传递给header
或redirect
才能实际设置它。-value
,则会返回Cookie的值。设置Cookie:
my $cookie = $cgi->cookie( -name => 'loggedin',
-value => '1',
-expires => '+4h',
-path => '/' );
print $cgi->header(-cookie=>$cookie);
检索Cookie:
my $cookie = $cgi->cookie('loggedin'); # Short for: $cgi->cookie( -name=>'loggedin' )
if ($cookie) {
# cookie "loggedin" was set
}
设置Cookie并重定向:
my $cookie = $cgi->cookie( -name => 'loggedin',
-value => '1',
-expires => '+4h',
-path => '/' );
print $cgi->redirect(
-uri => 'http://host.com',
-status => 302,
-cookie => $cookie
);
删除Cookie并重定向:
my $cookie = $cgi->cookie( -name => 'loggedin',
-value => '',
-expires => '-1d' );
print $cgi->redirect(
-uri => 'http://host.com',
-status => 302,
-cookie => $cookie
);
请注意,同时设置Cookie和重定向是依赖于浏览器和/或网络服务器。