PDA

View Full Version : Cookie problems


tdavis
08-31-2006, 12:45 AM
I can write cookies with no problems, because I can see them. But this script for some strange reason will not read them. I have two cookies. The first one is the number of orders placed, and the second one is the order details. The first one reads with no problems, but the second one will not work. Please help!

Here is the code:

use CGI ':standard';

$query = new CGI;
$cookie_in = $query->cookie("orders");
$itemcount = $cookie_in;

print "Content-type: text/html\n\n";
print "Number of orders: $itemcount";

for ($count=1; $count<$itemcount+1; $count++) {
$cartcookie = "cart" . $itemcount;
$cookie_in = $query->cookie("$cartcookie");
$itemstring = $cookie_in;
print "<br>Order $count: $itemstring<br><br>";
}

Thanks,
-tdavis

mlseim
08-31-2006, 04:34 PM
I didn't test this, but it looks like this might be it.

$cookie_in = $query->cookie("$cartcookie");

should be (no quotes):

$cookie_in = $query->cookie($cartcookie);

===========

Now, you didn't say how many orders there would be, but you do
have a limit on the number of cookies you can have. Say I put in
100 orders ... you'll have 100 cookies named cart1, cart2, cart3 ... cart100.

I don't think you can have that many cookies.

You might want to check into that.

tdavis
08-31-2006, 07:05 PM
Actually, I tried that, and I got the same results. But, now I am getting a different error. I changed the code again, trying to figure this out. Here is what I have now. My error is "Can't call method "cookie" on an undefined value". I dont know what is undefined.

use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use Data::Dumper;
use strict;

my $itemcount = 2; #for testing. need to get value from another cookie.
my $count;
my $query;
my $cart;
my $cookiein;
my $itemstring;

print "Content-type: text/html\n\n";
print "<br>Number of orders: $itemcount<br>";

for ($count=1; $count<$itemcount+1; $count++) {
my $cart = "cart" . $count;
my $cookiein = $query->cookie($cart);
$itemstring = $cookiein;
print "<br>Order $count: $itemstring<br><br>";
}

FishMonger
08-31-2006, 07:44 PM
The undefined value error is from not defining cgi $query object.

FishMonger
08-31-2006, 07:51 PM
It would be better to use server side session management for storing the shopping cart info instead of cookies.

http://search.cpan.org/~cwest/Apache-Session-1.81/Session.pm
http://search.cpan.org/~markstos/CGI-Session-4.14/lib/CGI/Session.pm
http://search.cpan.org/~markstos/CGI-Application-4.06/lib/CGI/Application.pm

tdavis
08-31-2006, 07:56 PM
Is this line not defining that value?
my $query;

tdavis
08-31-2006, 08:48 PM
Nevermind about the undefined $query.
I fixed that by adding this line:
my $query = new CGI;

I guess that was correct because I no longer get that error.
But, I still have the cookie problem.

Is there anyone out there that has any ideas?
Thanks!

FishMonger
08-31-2006, 09:23 PM
Try reading-in and building an array of the cookies before printing the headers.

tdavis
08-31-2006, 10:35 PM
I actually just figured out something. Apparently, the ampersands and the equal signs are causing the problem. I changed my script that saves the cookie to make those characters something else, and all of a sudden, it looks good. I dont get that! Should I make those a hex value?

tdavis
08-31-2006, 10:51 PM
I changed the ampersand to %26 and the equal sign to %3D and everything works perfectly. I had no idea that those characters could cause a problem.