Forum Moderators: coopster & phranque

Message Too Old, No Replies

Can't set cookie

Code works fine in one file, but not in another -- weird!

         

MichaelBluejay

10:26 pm on Apr 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My Perl code won't set a cookie, although the same code works fine in another file. I know the cookie hasn't been set because Internet Explorer 5 for Mac shows me that no cookie has been created (Preferences > Receiving Files > Cookies). If I run the original file then the cookie gets set fine.

You might wonder why I don't just use the file that works? The reason is the original file is part of a shopping cart that does different things than I need to do with the new file. Besides, if I copy the file that works to the new location and run it then it won't set the cookie! I have all the permissions set correctly and the script RUNS okay, it just won't set the cookie.

When I run the script from the Unix command line it sure looks like it's trying to set a cookie:


PROMPT>> perl cart.cgi
Set-Cookie: TinyWrists=12345; path=/; expires=Mon, 25-Apr-2005 22:00:36 GMT
Date: Sun, 25 Apr 2004 22:00:36 GMT
Content-Type: text/html; charset=ISO-8859-1

PROMPT>>

And here's the code I'm using. This is it, I stripped it down to almost nothing! This code works perfectly in the original file. All I changed was the "value", which is set dynamically in the original file. (Yeah, I tried copying in that dynamic code also, but when that didn't work either, I wanted to make my cookie-setting code as simple as possible.)


#!/usr/bin/perl -w

use CGI;
$cgi = new CGI;

$cookie = $cgi->cookie(-name=>'TinyWrists',
-value=>'12345',
-expires=>'+365d',
-path=>'/');

print $cgi->header(-cookie=>$cookie);
[/pre]

I've spent about eight hours on this problem so far. What the heck is going on?

Sorry I couldn't get all of the code snippets to show up in blue. I spent about half an hour on THAT....

Thanks for your help, -MBJ-

MichaelBluejay

12:15 am on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay, I made a major discovery, though my problem still isn't solved.

The cookie gets set properly if the script is at the top level of the domain (same place as index.html). But if it's two levels down, no cookie.

I know the problem can't be the "path" parameter because that's for reading cookies, not setting them. I have mine set to "/" anyway, which makes the cookies available to the whole website. And I'm not using the "domain" parameter anywhere.

I scoured Google with no luck. Why the heck will the cookie not set if the script that sets it isn't at the top level?

Thanks for your help, -MBJ-

MichaelBluejay

9:09 am on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay, yet another breakthrough discovery, although the problem isn't exactly solved....

My earlier statement about how it works if the file is at the top level wasn't entirely accurate. I thought moving the file to the top level was what fixed it, but actually what fixed it was running the CGI file directly, instead of including it as an SSI. So here's the summary:

METHOD 1: <index.html> calls <cart.cgi> via SSI, and <cart.cgi> tries to write the cookie --> WILL NOT SET COOKIE. No error, just that the cookie doesn't get set.

METHOD 2: Use just one file, <index.cgi>, which both sets the cookie and dynamically creates the whole page --> WORKS FINE.

Is this expected behavior? I sure would like to be able to write cookies by calling my CGI file via SSI.

Thanks, -MBJ-