# perl CGI to send a 302 Found response to caller plus setting # a cookie with name=XYZ123 and value 1. # the REDIRECT_URL points to the script described further below # hosted at my server. use strict; use warnings; use CGI; use CGI::Cookie; my $REDIRECT_URL = 'http://fotomazi.eu5.net/bin/200.cgi'; my $cgi = CGI->new; my $cookie = $cgi->cookie( -name=>'COOKIE302', -value=>'1', -expires=>'+4h', -path=>'/' ); print $cgi->redirect( -uri=> $REDIRECT_URL, -cookie => $cookie, -status=>302 ); exit(0); 1; __END__ #### # perl CGI which expects a cookie with name=XYZ123, if received # then cookie's value is incremented by 1 and returned. # this script can act as the final destination of the 302 Found redirect script above. use strict; use warnings; use CGI; use CGI::Cookie; my $cgi = CGI->new; my $cookies = fetch CGI::Cookie; my $cookie = $cookies->{'COOKIE302'}; if( ! defined($cookie) ){ print $cgi->header() . "No cookie received.
"; exit(1) } my $cookie_value = $cookie->value; if( ! ($cookie_value =~ /^([0-9]+)$/) ){ print $cgi->header() . "Invalid cookie received, value is '$cookie_value'.
"; exit(1) } $cookie->value($cookie_value+1); print $cgi->header( -charset => "utf-8", -cookie => $cookie, ) . "this is 200 OK, you have been redirected OK, your cookie is valid, value was '".$cookie_value."' and now is '".$cookie->value."'.
"; exit(0); 1; __END__ ##
## #!/usr/bin/env perl # create a UserAgent and send a request to 302.cgi to see how # it handles redirects. use LWP::UserAgent; use LWP::ConsoleLogger::Easy qw( debug_ua ); use HTTP::Response; use HTTP::Request::Common; use HTTP::Cookies; use URI; my $URLobj = URI->new('http://fotomazi.eu5.net/bin/302.cgi'); my $URLstr = $URLobj->as_string; my $ua = LWP::UserAgent->new( # 'requests_redirectable' => [], ); LWP::ConsoleLogger::Easy::debug_ua($ua, 6); $ua->cookie_jar(HTTP::Cookies->new( 'file' => './cookies.txt', 'autosave' => 1, 'ignore_discard' => 1, )); my %extra_headers = (); my $a_request = HTTP::Request::Common::GET( $URLstr, %extra_headers ); if( ! defined($a_request) ){ print STDERR "$0 : call to ".'HTTP::Request::Common::GET()'." has failed.\n"; exit(1) } my $a_response = $ua->request($a_request); print $a_response->as_string."\n";