Page 1 of 1

PHP Novice... double header errors...

Posted: Wed Dec 11, 2002 12:43 pm
by don_assi
Hi Guys,
I'm pretty new to all this PHP... and would appreciate it if anyone could help...
I'm getting the following error:
Warning: Cannot add header information - headers already sent by (output started at /home/gassi01/public_www/Cart/addCart.php:6) in /home/gassi01/public_www/Cart/addCart.php on line 14

When I run the below script:

Code: Select all

<?
require("Cart.php");
DBinfo();
Root();
mysql_connect("$DBHost","$DBUser","$DBPass");
mysql_select_db($DBName);
mysql_query("INSERT INTO CartItems VALUES
('$UID','$ItemID','$ItemQuantity','$Date','$CartItemsID')");
Header("Location: $Relative/viewCart.php?UID=$UID");
?>
The cart.php file that it links through to in order to access the DB is:

Code: Select all

<?
/* Replace these variables with information about your company */
$Company ="donassi co";
$Address1="555 Any St.";
$Address2="PO Box 3";
$City    ="London";
$State   ="UK";
$Zip     ="BR7 6EZ";
$Phone   ="+44 077 590 13806";
$Web     ="http://www.yoursite.com/";
$Email   ="don_assi@postmaster.co.uk";
$NoShipping = "Shipping Extra";
function Brand() &#123;
global $Company,$Address1,$Address2,$City,$State,$Zip,$Phone,$Web,$Email,$NoShipping;
&#125;

/* Replace these variables with information for connecting to your
database server */
$DBHost="hermes";
$DBUser="gassi01";
$DBPass="Capone77";
$DBName="gassi01db";
function DBInfo() &#123;
global $DBHost,$DBUser,$DBPass,$DBName;
&#125;

/* Replace these variables with the absolute and relative paths to your
MyCart scripts */
$WebRoot="home/gassi01/public_www/Cart";
$Relative="/~gassi01/Cart";
$WebHost="hermes.dcs.bbk.ac.uk";
function Root() &#123;
global $WebRoot,$Relative,$WebHost;
&#125;

function redFont($font,$text) &#123;
echo "<FONT FACE="$font" COLOR="red">$text</FONT>";
&#125;

function blueFont($font,$text) &#123;
echo "<FONT FACE="$font" COLOR="blue">$text</FONT>";
&#125;

function colorFont($color,$font,$text) &#123;
echo "<FONT FACE="$font" COLOR="$color">$text</FONT>";
&#125;

function fontFace($font,$text) &#123;
echo "<FONT FACE="$font">$text</FONT>";
&#125;

function fontSize($size,$color,$font,$text) &#123;
echo "<FONT FACE="$font" COLOR="$color" SIZE="$size">$text</FONT>";
&#125;

function commonHeader($Company,$title) &#123;
echo "<HTML><TITLE>$Company Shopping Cart - $title</TITLE><BODY BGCOLOR="#FFFFFF">";
&#125;

function receiptHeader($Company,$title) &#123;
echo "<HTML><TITLE>$Company Receipt - $title</TITLE><BODY BGCOLOR="#FFFFFF">";
&#125;

function commonFooter($Relative,$UID) &#123;
echo "<center><br><br><hr width="70%"><br>";
echo "<b><a href="$Relative/checkout.php?UID=$UID">Go To The Check Out Stand</a></b><br><br>";
echo "<b><a href="$Relative/viewCart.php?UID=$UID">View The Contents Of Your Cart</a></b><br><br>";
fontFace("Arial","<a href="$Relative/index.php?UID=$UID">Our Catalog</a>");
fontFace("Arial"," | <a href="/">Home</a>");
echo "<br><br></center></BODY></HTML>";
&#125;

function adminFooter($Relative) &#123;
echo "<center><br><br><hr width="70%"><br>";
fontFace("Arial"," | <a href="$Relative">Our Catalog</a>");
fontFace("Arial"," | <a href="$Relative/admin/">Shopping Cart Admin</a>");
fontFace("Arial"," | <a href="http://modems.rosenet.net/mysql/">MySQL Utilities</a>");
fontFace("Arial"," | <a href="http://www.rosenet.net/">Rosenet</a>");
echo "<br><br></center></BODY></HTML>";
&#125;

function receiptFooter($Company) &#123;
echo "<br><br><center>";
fontFace("Arial","Thank you for ordering from <b>$Company</b>");
echo "</center><br><br>";
&#125;

?>
So it seems that the information is being sent twice... is there anything wrong with the script?

Posted: Thu Dec 12, 2002 11:09 am
by Traduim
If you look Cart.php you'll see there are some echoes in it.

Headers can only be sent when nothing else have been sent to client's browser.

If you want to use header(), you must get rid of all echoes and other printable stuff (including white spaces and line feeds out of <? ?>)

Header Errors

Posted: Sat Dec 14, 2002 9:34 am
by don_assi
Thanks,

But I'm not sure how I am going to get around this... how would I be able to link to the cart.php (the main file) through the require statement and at the same time link through to the next page (currently through the 'Header' statement?

Thanks
don_assi

Posted: Sat Dec 14, 2002 10:25 am
by nathus
Don't think its the echo statements that are giving you the problem, as the functions that contain them aren't called. However, I have a question as to what the DBInfo, Brand, and Root functions are for?

Posted: Sat Dec 14, 2002 10:51 am
by nathus
i played around with the code a bit, and got it to work I think. there's a space after the ?> in cart.php thats messing it up. (at least thats what fixed it for me).

Posted: Sat Dec 14, 2002 2:17 pm
by hob_goblin

Code: Select all

function Brand() &#123; 
global $Company,$Address1,$Address2,$City,$State,$Zip,$Phone,$Web,$Email,$NoShipping; 
&#125;
and

Code: Select all

function DBInfo() &#123; 
global $DBHost,$DBUser,$DBPass,$DBName; 
&#125;
and

Code: Select all

function Root() &#123; 
global $WebRoot,$Relative,$WebHost; 
&#125;
are unecissary, when you include a file, all the variables defined will merge into the current scope, there is no need to do anything like that.