Jump to content

Clearing shopping cart from session (cookie)


asy1mpo

Recommended Posts

I have used the shopping cart thats on the Adobe site (http://www.adobe.com/devnet/dreamweaver/articles/php_cart_5.0.html)

 

It all works fine except now I need to clear the basket once the order has gone through

 

How can this be done?

 

I have used code for another similar site I have done before but it doesnt clear it. I think its using sessions/cookies differently but dont know really

 

The code I used for other site is

 

<?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();

 

Any help would be great

 

Thanks

Link to comment
Share on other sites

Well - I am not sure how you are keeping track of your items...

If you have individual items stored into session variables and then a process counting them and then setting.

 

$_SESSION['count'];

 

Then unsetting that doesn't help.

 

How are you going about this?

Link to comment
Share on other sites

<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
foreach ($_SESSION as $key => $val) {
    unset($_SESSION[$key]);
}
unset($_SESSION);
setcookie(session_id(), '', time()-344444*1000, '/', 'yourdomain.com');

?> 

 

Should kill all session data.

Link to comment
Share on other sites

Well - I am not sure how you are keeping track of your items...

If you have individual items stored into session variables and then a process counting them and then setting.

 

$_SESSION['count'];

 

Then unsetting that doesn't help.

 

How are you going about this?

 

The cart table is like this:

 

+--------+----------------------------+--------+----+

| cartId | cookieId                  | itemId | qty |

+--------+----------------------------+--------+----+

|      1 | 5eu9khhbu6cpaqp4kibvn839b4 |    49 |  15 | 

|      2 | 5eu9khhbu6cpaqp4kibvn839b4 |    61 |  13 | 

|      3 | 5eu9khhbu6cpaqp4kibvn839b4 |    21 |  1 | 

+--------+----------------------------+--------+----+

 

 

Link to comment
Share on other sites

If you unset all session variables you will effectivley log the user out as well as lose any other information you were keeping track of.

 

Ideally only want to kill the session for the cart and not everything. Its OK for this site but a future site will have some registering and logging on etc

Link to comment
Share on other sites

<?php
session_start();
// Use $HTTP_SESSION_VARS with PHP 4.0.6 or less
foreach ($_SESSION as $key => $val) {
    unset($_SESSION[$key]);
}
unset($_SESSION);
setcookie(session_id(), '', time()-344444*1000, '/', 'yourdomain.com');

?> 

 

Should kill all session data.

 

Do I need to change anything???

 

I created a new page called clear.php and ran it and went back to cart and its still get the items in it!?!?!?

Link to comment
Share on other sites

If it helps here is the code for the cart session

 

function GetCartId()
{
	// This function will generate an encrypted string and
	// will set it as a cookie using set_cookie. This will
	// also be used as the cookieId field in the cart table

	if(isset($_COOKIE["cartId"]))
	{
		return $_COOKIE["cartId"];
	}
	else
	{
		// There is no cookie set. We will set the cookie
		// and return the value of the users session ID

		session_start();
		setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
		return session_id();
	}
}

Link to comment
Share on other sites

Got it working

 

<?php
//kill session
// 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();

//kill cookie
// set the expiration date to one hour ago
setcookie ("cartId", session_id(), time() - 3600);
return session_id(); 
?>

 

Just having problems now with SecPay related stuff, when it goes to the authorised page it doesnt seem to run the kill session code. Anyway, thats another thing.........

 

Thanks for your help guys

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.