markarita wrote in php 😡pissed off

Setting cookies, it never works right.

I feel stupid, but I have never been able to use cookies in a way I want to in PHP. It never works, somehow headers are always mysteriously sent before my setcookie(); function tries to do something. I know it is possible to set one not at the absolute top of the file, where it is completely useless to me. I've tried using ob_start() and ob_end_flush to control output, but that wont't work here either.


Part of a shopping cart draft that won't work.

class cart {
var $items;
var $cookielist;
var $itemlist;
function makecookies() {
setcookie('bead_cart', '', time()+60*60*12);
}
function usecookies() {
if(isset($_COOKIE['bead_cart'])) {
$this->cookielist = explode(',', $_COOKIE['bead_cart']);
} else {
$this->makecookies();
$this->cookielist = '';
}
return $this->cookielist;
}
function updatecookies($action, $item) {
if($action == 'add') {
if(strpos($this->itemlist, ',') === false) {
$this->itemlist .= ','.$item;
} else {
$this->itemlist = $item;
}
setcookie('bead_cart', '', time() - 3600);
setcookie('bead_cart', $this->itemlist, time()+60*60*3);
}
elseif($action == 'subtract') {
$itemarray = explode(',', $this->itemlist);
$count = count($itemarray);
for($i=0; $i<=$count; $i++) {
if($itemarray[$i] == $item) {
$itemarray[$i] = '';
}
}
$this->itemlist = implode(',', $itemarray);
setcookie('bead_cart', '', time() - 3600);
setcookie('bead_cart', $this->itemlist, time()+60*60*3);
}
}
function cart() {
//$this->items = array();
$this->items = $this->usecookies();
$this->itemlist = $_COOKIE['bead_cart'];
}

etc...the rest doens't matter.

$cart = new cart;
$cart->makecookies();

Does nothing. Help.