Hi I'm currently writing a shopping cart style program which allows users to login , purchase songs and then logout with their balance being decremented depending on whether they purchased anything. I have a database called sessions.db which keeps track of their cookie id (a random number), their username, contents that they've purchased and their balance.
At the moment my program creates a cookie if there are none already on the pc and lets them do all the above just fine.
However, if another user logs on, then my if statement says "If there is a cookie on this machine, use those details". Therefore the cookie created by the first user is always used. I'm sure that it has something to do with my if statement and i have to somehow look into the database and say "If there is a cookie on this computer and it's value is the same as a cookie id in the database, then use that cookie".
This is the code I have atm. You will see my if statement is "if ($cookie) " which is only looking to see if a cookie is on the pc.
sub set_cookie {
my $dbh = DBI->connect("DBI:SQLite:sessions.db")
or die "Cannot connect: " . $DBI::errstr;
$cookie = $q->cookie('cart_id');
if ("$cookie") {
print $q->header;
} else {
$cart_id = int(rand 1e9);
$cookie = $q->cookie( -name => "cart_id",
-value => "$cart_id",
-expires => "+6M" );
$sth = $dbh->prepare("insert into sessions values (?, ?, 0, 50)") or die "Cannot prepare";
$sth->execute($cart_id, $username) or die "Cannot execute";
$sth->finish;
print $q->header(-cookie=>$cookie);
$cookie = $cart_id;
}
}
Is someone able to give me a hand on how to do this keeping in mind i don't really want to change my code a whole lot.
Cheers everyone
At the moment my program creates a cookie if there are none already on the pc and lets them do all the above just fine.
However, if another user logs on, then my if statement says "If there is a cookie on this machine, use those details". Therefore the cookie created by the first user is always used. I'm sure that it has something to do with my if statement and i have to somehow look into the database and say "If there is a cookie on this computer and it's value is the same as a cookie id in the database, then use that cookie".
This is the code I have atm. You will see my if statement is "if ($cookie) " which is only looking to see if a cookie is on the pc.
sub set_cookie {
my $dbh = DBI->connect("DBI:SQLite:sessions.db")
or die "Cannot connect: " . $DBI::errstr;
$cookie = $q->cookie('cart_id');
if ("$cookie") {
print $q->header;
} else {
$cart_id = int(rand 1e9);
$cookie = $q->cookie( -name => "cart_id",
-value => "$cart_id",
-expires => "+6M" );
$sth = $dbh->prepare("insert into sessions values (?, ?, 0, 50)") or die "Cannot prepare";
$sth->execute($cart_id, $username) or die "Cannot execute";
$sth->finish;
print $q->header(-cookie=>$cookie);
$cookie = $cart_id;
}
}
Is someone able to give me a hand on how to do this keeping in mind i don't really want to change my code a whole lot.
Cheers everyone