Ending PHP Session

Hi guys,
I am currently building a shopping cart. I have done all of the logic up to the point where the items bought are summed up and ready to send to the payment gateways. But I have a problem.

My shopping cart doesnt require that a user log in to shop, it assigns a session to that user at the beginning and passes that session all along the pages till he gets to checkout.

Now I want to end this user’s session as soon as he gets to checkout so that any other buyer who comes in on that browser after him will meet an empty shopping cart. I have not been able to achieve that either with session_destroy() or unset_session().

Please kindly help me. I appreciate:)

unset($_SESSION[‘cart’]); after processing the order

I use $_SESSION and try to emulate a new user with no $_SESSION set.

I have tested the Global $_SESSION array variable and clear the contents like this:



<?php
 #before clearing
 echo '<pre>';
   print_r( $_SESSION );
 echo '</pre>';

 $_SESSION = array();

 #after clearing
 echo '<pre>';
   print_r( $_SESSION );
 echo '</pre>';


I don’t remember where I found this ages ago, the documentation I think, anyway it might help even if I can’t decipher my own notes :shifty:

  1. Destroying a session with $_SESSION

<?php
// Initialize the session.
// If you are using session_name(“something”), don’t forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it’s desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), ‘’, time()-42000, ‘/’);
}

// Finally, destroy the session.
session_destroy();
?>
unset ($_SESSION[‘varname’]);.