asy1mpo Posted July 10, 2007 Share Posted July 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
shamilton Posted July 10, 2007 Share Posted July 10, 2007 Here ya go: http://us.php.net/session Take heed to the warnings inside this page. Quote Link to comment Share on other sites More sharing options...
asy1mpo Posted July 10, 2007 Author Share Posted July 10, 2007 So I use <?php session_start(); // Use $HTTP_SESSION_VARS with PHP 4.0.6 or less unset($_SESSION['count']); ?> I have just tried and it doesnt clear the cart???? Quote Link to comment Share on other sites More sharing options...
shamilton Posted July 10, 2007 Share Posted July 10, 2007 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? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
shamilton Posted July 10, 2007 Share Posted July 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
asy1mpo Posted July 10, 2007 Author Share Posted July 10, 2007 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 | +--------+----------------------------+--------+----+ Quote Link to comment Share on other sites More sharing options...
asy1mpo Posted July 10, 2007 Author Share Posted July 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
asy1mpo Posted July 10, 2007 Author Share Posted July 10, 2007 <?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!?!?!? Quote Link to comment Share on other sites More sharing options...
asy1mpo Posted July 10, 2007 Author Share Posted July 10, 2007 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(); } } Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 Just use the setcookie function I posted except with cartId as the name and maybe remove the domain and the slash part. See if that works. Quote Link to comment Share on other sites More sharing options...
asy1mpo Posted July 10, 2007 Author Share Posted July 10, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.