destroying a session help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

destroying a session help

Post by C_Calav »

hi guys,

i have this piece of code

Code: Select all

ob_start();
session_start();
$_SESSION = array(); // unset all of the session variables
session_destroy();
unset($cartId);
to destroy this session

Code: Select all

// There is no cookie set. We will set the cookie 
// and return the value of the users session ID 
setcookie("cartId", session_id(), time() + (3600)); 
return session_id();
now theoretically, if i go to my cart after i have been to a page with the destroy session code on it, i should have no items in my cart right?

or am i wrong.... ?

i dont think my session is getting destroyed as i still have items in cart (and i can see it in my database, as i am adding it)

can anyone help me out with destroying my session when the user goes to a certain page??

thanx heaps
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

should i re-phrase my question?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Yes
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

on my website http://www.deskjetmodels.co.nz when you add a item to the cart you get issued with a cookieID.

now that cookieID holds your items in your cart.

after the user has completed a order and the transaction is approved i want the users shopping cart to be empty and the cookieID cleard.

i want this to happen because if they make another order they will still have the same cookieID.

my question is how do i clear the users cookieID and start off 'fresh' again like they have just visted the site for the first time.

thanx.

i have some code supplied above, that i have in place at the moment to try and do this
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

you have set a cookie

Code: Select all

setcookie("cartId", session_id(), time() + (3600));
now, you need to delete it by setting the cookie past the time of the cookie being first created - by setting it back to an hour ago; hence, the cookie is expired.

Code: Select all

setcookie("cartId", session_id(), time() - (3600));
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hi there,

thanx for your reply hongco.

i put your bit of code at the top of the page where i want the session to end.

Code: Select all

ob_start();
setcookie("cartId", session_id(), time() - (3600));
and it doesnt seem to be working. the user still has the same session running :?

any idea's?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You'll need to delete the cookie / destroy the session and the refresh the browser.

A better method than refreshing is to point to a destroy.php script which destroys the session and then uses a header() to send the user back to the required location.
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

hi thanx for your reply,

im having a bit of trouble trying to get this script going.

at the top of my page i have this

Code: Select all

ob_start();
header('Location: http://www.deskjetmodels.co.nz/destroy.php');
and my destroy.php i have this

Code: Select all

ob_start();
setcookie("cartId", session_id(), time() - (3600)); 

header('Location: http://www.deskjetmodels.co.nz/success.php');
now i am getting a page not found error.

can anyone see if i am doing anything wrong? 8O
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ok,

i think i know why it is not working.

the user goes from my site to a 'hosted payments page' a transaction happens and then they get directed back to my site and to the page success.php

now the success.php page has this address in the address bar:

/success.php?result=545m89w34589v278trq4896qb6903568b905v8304680b95....

etc

because it came from a 'secure' page and is sending specfic data back to the success.php page for me to use again.

now it is on this page that i need to end the users session.

i got told i need to have a refresh before my session is deleted. now i used the following bit of code at the top of the page

Code: Select all

ob_start();header('Location: http://www.deskjetmodels.co.nz/destroy.php');
and on the destroy.php page:

Code: Select all

ob_start();setcookie("cartId", session_id(), time() - (3600));  header('Location: http://www.deskjetmodels.co.nz/success.php');
now i get page not found.

does anyone have anyidea's what to do or how i can achieve destroying a session?

im getting muddled up with

/success.php?result=545m89w34589v278trq4896qb6903568b905v8304680b95....

this page 8O :?

thanx guys
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

Hi guys,

i have just been doing some more work on it and it goes to the destroy.php page fine, its the header that redirects the user back to the success page is where there is some kind of error.

i guess this has something to do with the long string of numbers like in the above post i made??

thanx for help
Post Reply