destroying a session

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

Post 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
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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();
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanx man,

normally i copy and paste words.. decided to write it and look what happened :roll:
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post 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
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
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 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

ob_start(); has to be on the very 1st line of the file, not after a bunch of code.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply