Can't delete cookie

Hi there,

So I’m making a shopping cart. Each cart is identified by a user_session_id which is stored in a cookie like so:

if(isset($_COOKIE['SESSION'])) $uid = $_COOKIE['SESSION'];
else $uid = md5(uniqid('biped', true));

setcookie('SESSION', $uid, time()+(60*60*24));

Once the transaction is completed I need to delete the cookie and destroy the session, which I do like so:

$_SESSION = array();
setcookie('SESSION', '', time() - 3600);

But it doesn’t seem to want to work. When I initialise a new shopping cart it is given the same user_session_id as the previous one. This is bad.
What am I doing wrong? My guess is the line that even though I have set the cookie to a time in the past, the browser has not expired it by the time the new cart is initialised. Do I need to unset the cookie as well? Like so:

$_SESSION = array();
setcookie('SESSION', '', time() - 3600);

Many thanks,

In what order are these code blocks? I’m assuming Block 1 executes on every page load. If Block 2 executes before Block 1, you wont delete the cookie, because $_COOKIE[‘SESSION’] still exists, and Block 1 will reinstate the cookie as-was.

Block 3 replacing Block 2 should solve the issue.