...

View Full Version : setcookie() vs 'Cannot modify header ' problem



serap
03-24-2004, 09:21 PM
I think I have an easy problem but cannot get over it somehow...
I get the classical:

Warning: Cannot modify header information - headers already sent by (output started at /wn/show_item_list.php:6) in /wn/scripts/db2.php on line 25

my code is as follows:

for - show_item_list.php -

<?php include("db2.php");?>
<html>
<head>
<title>Order Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.boxes {...
....
...html stuff

for - db2.php -

<?php
// This page contains the connection routine for the
// database as well as getting the ID of the cart, etc
session_start();
$connection=mysql_connect("host", "dbuser", "dbpass")
or die(mysql_error());
mysql_select_db("DB", $connection);

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
setcookie("CartID", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
?>

The db2.php is at the top of the show_item_list.php - so I do not send anything (html code, or echo's ) to the browser - Im new so I might have missed something?

Thanks,

sidney
03-24-2004, 10:27 PM
your outputing data bfeore cookie is set

the cook must be set before this is sent to browser

<html>
<head>
<title>Order Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.boxes {...
....
...html stuff

serap
03-24-2004, 10:41 PM
You mean then - that the setcookie() in the include file db2.php is ran after the html stuff below EVEN that it is at the top of the code?


<?php include("db2.php");?>
<html>
<head>
<title>Order Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--

thanks...

sidney
03-24-2004, 11:19 PM
no sorry thought it was one script

is there any white space a top of script

firepages
03-25-2004, 12:58 AM
GetCartId() is a function and ignored until called ... where are you calling that function ? chnaces are after you have output some HTML

less likely is that your script is generating an error before the function is called , e.g. if your mysql_connect() failed ,output would be created, same for any error or notice that your code generates , if it generates the error before the cookie is set you get the above problem.

serap
03-25-2004, 07:48 AM
yep Firepages, I think I got it now. I call the GetCartId function way down when I output the sql select stuff - and that wont work! So i need to call it on top and put the value from getcartId to a variable - that shoul probably do it!

Ive also heard that using ob_start() could get rid of these types of errors - but dont know what that statement may effect.

Thanks a lot...

firepages
03-25-2004, 08:46 AM
Hi , yes ob_start() at the top of the page will supress any content until flush()ed either explicitly in your script or implicitly when the script execution ends.

regardless its better to find out exactly what the problem is first (as you have done) and try and work around it rather than use what would otherwise be a hack.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum