Page 1 of 1
destroying a session
Posted: Wed Apr 20, 2005 10:21 pm
by C_Calav
hi guys,
im writing somer code to destroy the user's session after a order has been completed (or failed)
would this code here
Code: Select all
<?
obstart();
session_start();
$_SESSION = array(); // unset all of the session variables
session_destroy();
unset($cartId);
?>
delete this session i have set here: ??
Code: Select all
setcookie("cartId", session_id(), time() + (3600));
as i was testing it looked as if it was working, then i got this error
Fatal error: Call to undefined function: obstart() in
have i specified obstart in the wrong place or something?
thanx for you help
Posted: Wed Apr 20, 2005 10:27 pm
by hongco
it said that the function was not defined - meaning it does not exist.
ob_start(); // note: underscore between ob and start

change yours to ob_start();
Posted: Wed Apr 20, 2005 11:00 pm
by C_Calav
thanx man,
normally i copy and paste words.. decided to write it and look what happened

Posted: Wed Apr 20, 2005 11:10 pm
by C_Calav
ok have changed and tested and now get this.
but i thought ob_start was ment to eliminate this problem?
Warning: session_start(): Cannot send session cache limiter - headers already sent
thanx for the help
Posted: Thu Apr 21, 2005 1:16 am
by hongco
do you include this file in any other file, if you do, make sure you don't have any output prior to calling ob_start; however, if you need them there, then they should be inside ob_start() as well.
Posted: Thu Apr 21, 2005 10:25 am
by pickle
I've never used ob_start when setting sessions, so it might be superfluous. The error you're getting is because something's been sent before you goof with the session. A common problem is having a space or a blank line before the "<?PHP" declaration
Posted: Thu Apr 21, 2005 4:10 pm
by C_Calav
hi guys thanx for your help,
hongco - i do not include this file in any other file.
pickle - here is some of the code i have above it, might be a problem with where i have the code placed.
This script i have is at the bottom of a summary page after a transaction has occured. i have a mail() then i want to end the session.
Code: Select all
....
....
....
$msg .= "Address 1: $O_Address1\n";
$msg .= "Address 2: $O_Address2\n";
$msg .= "City: $O_City\n";
$msg .= "Freight: $O_Freight";
$query1 = "SELECT * FROM cart where cookieId = '$O_CookieID'";
$result1 = mysql_query($query1) or die (mysql_error()."<br />Couldn't execute query: $query1");
$num_results = mysql_num_rows($result1);
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result1);
$itemId = $row['itemId'];
$qty = $row['qty'];
$msg .="\n\nItemID: $itemId\n";
$msg .="qty: $qty";
}
mail($recipient, $subject, $msg);
ob_start();
session_start();
$_SESSION = array(); // unset all of the session variables
session_destroy();
unset($cartId);
Warning: session_start(): Cannot send session cache limiter - headers already sent
Posted: Thu Apr 21, 2005 5:18 pm
by shiznatix
ob_start(); has to be on the very 1st line of the file, not after a bunch of code.
Posted: Thu Apr 21, 2005 6:07 pm
by pickle
Ya, what ob_start does is it stops anything being sent to the browser, from that point on. It doesn't stop anything from happening before it's called. My suggestion would be to pull the data you need out of $_SESSION right at the top of the page, then destroy the session before anything is sent to the browser.