RE: [PHP] headers already sent and cookie problem

From: Date: Fri, 18 Jul 2003 16:19:52 +0000
Subject: RE: [PHP] headers already sent and cookie problem
Groups: php.general 
Request: Send a blank email to php-general+get-155801@lists.php.net to get a copy of this message
-----Original Message----- From: frederik feys To: 'Ford, Mike [LSS]'; php-general@lists.php.net Here's the URL: http://www.aurelis.org/store/cart.txt and the get_cartID: http://www.aurelis.org/store/includes/functions/get_cartID.txt -------------------------- I've only had time for a quick look, but I think I have an inkling of where your problem might be. It all seems to lie with the way you've implemented GetCartId() -- and the problem will only show up any time the cartId cookie is not already set. Basically, if the cookie is not set, GetCartId() generates a new ID and sets the cookie -- but this requires sending a header, and you can't do this once you've started output of the real page. So you must make you first call to GetCartId() *before* you send any output. Now, if we look at this fragment of code from /store/cart.txt: function ShowCart($lang) { global $connection,$lang_dir,$DOCUMENT_ROOT,$page_theme,$country_iso_code,$srch_ter m; /* ob_start(); //start buffering output */ $page_title=get_label(194,$lang,$connection); $page_tab=3; include_once($DOCUMENT_ROOT . "/header_aurelis.php"); $country_iso_code = $_GET[country_iso_code]; if($lang=="EN"){ $currency_symbol = "$ "; } else{ $currency_symbol = "€ "; } $totalCost = 0; $show_cart = @mysql_query(("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc"),$connection); ... we can see that you include header_aurelis.php, which presumably outputs the initial part of your HTML, before you do $show_cart = @mysql_query ... ; which includes a call to GetCartId() -- which will attempt to set the cartId cookie if it's not already set, and will fail with the "headers already sent" error. So, overall, I'd say you've got to get/set your cartId earlier in the game -- and certainly before you call or include anything that outputs actual page content. This analysis also explains why the error is intermittent -- someone who has set the cartId cookie once will probably not see the error again until after the cookie expires 30 days later! Hope this helps (and you followed my somewhat rambling explanation!) Cheers! Mike

« previous php.general (#155801) next »