Gossamer Forum
Home : General : Perl Programming :

Problem with Perl Cookie Set

Quote Reply
Problem with Perl Cookie Set
I can't seem to get Perl scripts to set cookies on my server, altough I know the code is right since I tested on another server and cookie sets fine. However on this particular server, the scripts just don't set the cookie! Also I know that the server is able to set cookies since I tested with a php syntax

<?php

setcookie("name", $name, time()+30000000);

?>

and cookie sets fine! Any suggestion? Maybe it's some part of perl configuration that is malfunctioning with cookie setting? Anyone have any ideas, it'll be lifesaving!!

Quote Reply
Re: [xpert] Problem with Perl Cookie Set In reply to
If you are using mod_perl together with Apache versions 1.3.23 or 1.3.24, then it might be a bug in mod_proxy... otherwise it don't know.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Problem with Perl Cookie Set In reply to
Good idea, anyway, i disabled modperl, and restarted server, however the cookie setting is still not working. So i assume it's not mod_perl isssue!
Quote Reply
Re: [xpert] Problem with Perl Cookie Set In reply to
Seeing the code you are using would be helpful :)
Quote Reply
Re: [Paul] Problem with Perl Cookie Set In reply to
Code:
#!/usr/bin/perl -w

use strict;

my($expDate) = "Wednesday, 09-Nov-03 00:00:00 GMT";

my($theDomain) = ".engr.iupui.edu";

my($path) = "/cgi-bin/";

setCookie("user", "dbewley", $expDate, $path, $theDomain);

setCookie("user_addr", $ENV{'REMOTE_HOST'}, $expDate, $path, $theDomain)

if defined($ENV{'REMOTE_HOST'});

setCookie("flag", "black", $expDate, $path, ".iupui.edu");

setCookie("car", "honda:accord:88:LXI:green", $expDate, $path, $theDomain);

my(%cookies) = getCookies();

print("Content-type: text/html\n\n");

print("<HTML>");

print("<HEAD><TITLE>The Cookie Display</TITLE></HEAD>");

print("<BODY>");

print("<H1>Cookies</H1>");

print("<TABLE BORDER=1 CELLPADDING=10>");

foreach (sort(keys(%cookies))) {

print("<TR><TD>$_</TD><TD>$cookies{$_}</TD></TR>");

}

print("</TABLE>");

print("</BODY>");

print("</HTML>");



sub setCookie {

my($name, $val, $exp, $path, $dom, $secure) = @_;

print("Set-Cookie: ");

print("$name=$val, expires=$exp, path=$path, domain=$dom");

print(", $secure") if defined($secure);

print("\n");

}

sub getCookies {

my(%cookies);

foreach (split (/; /,$ENV{'HTTP_COOKIE'})){

my($key) = split(/=/, $_);

$cookies{$key} = substr($_, index($_, "=")+1);

}

return(%cookies);

}

Here's another simple cookie script I tested on the server with no luck:

#!/usr/bin/perl -w

# cc-set.cgi - set cookie with CGI::Cookie

use CGI qw(:standard);

use CGI::Cookie; # this line is new

unless (param()) {

# display form

print

header(),

start_html("Cookie Baker"),

h1("Cookie Baker"),

start_form(),

p("What's your name?", textfield("NAME")),

submit(),

end_form(),

end_html();

} else {

# process form and set cookie

$name = param("NAME");

$to_set = CGI::Cookie->new(-name => "username",

-value => $name,

-expires => "+30s",

); # this has changed

print

header(-cookie => $to_set),

start_html("Thanks!"),

h1("Thanks for using the Cookie Baker"),

p("I set your name to ", b($name),

" and I will remember this if you visit ",

a({-href => "cc-get.cgi"}, "here"),

" within the next 30 seconds."),

end_html();

}

Last edited by:

Wil: Aug 25, 2002, 7:02 AM
Quote Reply
Re: [xpert] Problem with Perl Cookie Set In reply to
Yikes.

Try this:

Code:

use CGI;

my $IN = new CGI;
my $ck = $IN->cookie('Test') ? ($IN->cookie('Test') + 1) : 0;
my $CK = $IN->cookie( -name => 'Test', -value => $ck, -expires => '+1y', -path => '/' );

print $IN->header( -cookie => $CK );
print "You have been here @{ [ $IN->cookie('Test') ] } time(s). Refresh the page.";
Quote Reply
Re: [Paul] Problem with Perl Cookie Set In reply to
Strange, Paul's code sets cookie, and my don't? What's the secret behind this Paul? I tested my scripts from localhost apache and works. But fails to work when runs from my web acpahe server? Please shed some light among me;)
Quote Reply
Re: [xpert] Problem with Perl Cookie Set In reply to
Oh also,forgot to mention Paul, your counter don't work even after refresh page!
Quote Reply
Re: [xpert] Problem with Perl Cookie Set In reply to
Thats just my sucky code's fault, this one works:

Code:
my $IN = new CGI;
my $ck = defined $IN->cookie('Test') ? $IN->cookie('Test') + 1 : 0;
my $CK = $IN->cookie( -name => 'Test', -value => $ck, -expires => '+1y' );

print $IN->header( -cookie => $CK );
print "You have been here $ck time(s). Refresh the page.";

>>
Strange, Paul's code sets cookie, and my don't? What's the secret behind this Paul?
<<

No secret just well written code (second time ..*cough*)

Last edited by:

Paul: Aug 25, 2002, 7:28 AM
Quote Reply
Re: [Paul] Problem with Perl Cookie Set In reply to
You know what's the deal with why my cookie codes work in other servers, and not this particular one while you code all in all:)?
Quote Reply
Re: [xpert] Problem with Perl Cookie Set In reply to
There could be numerous reasons, one of which is that in your example the cookie expires 30 seconds after it is set Wink
Quote Reply
Re: [Paul] Problem with Perl Cookie Set In reply to
Got it finally, the problem was that i was tryin to pull a cfg value from a plugin, and the expire can't seem to take in the value. After I redefined the expire param, everything works fine, but anyway Paul, you know why this don't work? Just too strange when it worked on other servers!!

use Links::Plugins;

my $opts = Links::Plugins->get_plugin_user_cfg ('IOVote');

my $c = $IN->cookie (-name => 'IHsource_id', -value => $id,-expires=>time+$opts->{outgoing_timelimit}*60*60);

print $IN->header ( -cookie => $c );



Quote Reply
Re: [xpert] Problem with Perl Cookie Set In reply to
Because your expires field is invalid :)

Checkout the CGI.pm docs:

http://www.perldoc.com/...GI.html#HTTP-COOKIES

and

http://www.perldoc.com/.../lib/CGI/Cookie.html

Last edited by:

Paul: Aug 25, 2002, 9:13 AM