Page 1 of 3

Need Help... Please..

Posted: Tue Jan 28, 2003 10:09 am
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:5) in /home/gassi01/public_www/Cart/addCart.php on line 13

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?

Really would appreciate your help...
Thanks
don_assi

Posted: Tue Jan 28, 2003 10:40 am
by puckeye
Do you have any spaces or carriage returns at the top of either of the files? Does the server return any errors before the header is sent?

Posted: Tue Jan 28, 2003 10:42 am
by puckeye
Oh yeah you should also read this viewtopic.php?t=1157.

Also try to make your thread subjects more precise...

Posted: Tue Jan 28, 2003 11:08 am
by don_assi
Thanks Puckeye.. the thread you sent was interesting..
But the answer to both your questions is No.

I have made sure there are no spaces or Carriage Returns... and no errors are being sent by the server before the header is sent.

What do you think it could be?

Appreciate your help...

Posted: Tue Jan 28, 2003 11:57 am
by DeGauss
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");

Look inside cart.php. You'll notice that there are a few echo statements (for pushing out font colors, etc).

You can't echo or print before sending headers.

Posted: Wed Jan 29, 2003 12:58 pm
by don_assi
Thanks DeGauss,
But not sure how I am meant to get around this... I tried switching them around... but got an error as well. Any Ideas?

Thanks
don_assi

Posted: Wed Jan 29, 2003 2:03 pm
by evilcoder
Here ya solution. What ever page your trying to send headers in the script put this at the far top and far bottom of the page:

Top of page:

<?php

ob_start();

?>

Bottom:

<?php

ob_end_flush();

?>

That way you can send headers throughout your whole page until your hearts content.

Posted: Wed Jan 29, 2003 2:40 pm
by oldtimer
Yes that works great. I use it in conjunction with cookies and sessions.

Posted: Wed Jan 29, 2003 4:38 pm
by hedge
evilcoder wrote:Here ya solution. What ever page your trying to send headers in the script put this at the far top and far bottom of the page:

Top of page:

<?php

ob_start();

?>

Bottom:

<?php

ob_end_flush();

?>

That way you can send headers throughout your whole page until your hearts content.
That's a bandaid solution.

Posted: Wed Jan 29, 2003 6:02 pm
by Nik
When i used those 2 functions u describes then the server immediately was start giving me the file without echoin nothing that i had before the header command!!

Why?

Posted: Thu Jan 30, 2003 1:30 am
by evilcoder
Hey hedge. When you cut yourself deep, would you rather stop the bleeding or just let it drip away?

ob_start() and ob_end_flush() are not bandade solutions, they were created for a reason, to allow headers to be sent throughout the page, and if you can find another way to do it, that is that EASY and doesnt effect page performance, i'd like to hear it. But until then accept VALID and CORRECT solutions, UNLESS once again, you can provide a better solution.

Posted: Thu Jan 30, 2003 2:47 am
by don_assi
Cheers.. guys... (evilcoder)
I'm gonna have a go at it later on...

don_assi

Posted: Thu Jan 30, 2003 3:41 am
by Nik
what do u mean by saying ob_start() and ob_end_flush() are not bandade solutions?!?!

Well i did find another solution and hopefully it woks great to me!

echo '<meta http-equiv="refresh" content="5;url=http://tigani.coderz.gr/~nik/LogReader.exe" />';

;)

Posted: Thu Jan 30, 2003 4:22 am
by evilcoder
yes thats nice, however some people have meta redirecting turned off in their browser settings. So therefore using headers is far more effective.

Posted: Thu Jan 30, 2003 8:59 am
by hedge
evilcoder wrote:Hey hedge. When you cut yourself deep, would you rather stop the bleeding or just let it drip away?

ob_start() and ob_end_flush() are not bandade solutions, they were created for a reason, to allow headers to be sent throughout the page, and if you can find another way to do it, that is that EASY and doesnt effect page performance, i'd like to hear it. But until then accept VALID and CORRECT solutions, UNLESS once again, you can provide a better solution.
All I meant is that I would find the place where output is being sent previous to the header command. If you look hard enough you can always find it. BTW that is not why the object buffering functions where added. Using the object buffering in this way is a work-around for lazy programmers that don't want to find their bugs.