Need Help... Please..

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

don_assi
Forum Newbie
Posts: 16
Joined: Tue Dec 03, 2002 7:40 am

Need Help... Please..

Post 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
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post 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?
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

Oh yeah you should also read this viewtopic.php?t=1157.

Also try to make your thread subjects more precise...
don_assi
Forum Newbie
Posts: 16
Joined: Tue Dec 03, 2002 7:40 am

Post 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...
DeGauss
Forum Contributor
Posts: 105
Joined: Tue Oct 22, 2002 9:44 am
Location: Gainesville, FL

Post 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.
don_assi
Forum Newbie
Posts: 16
Joined: Tue Dec 03, 2002 7:40 am

Post 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
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Yes that works great. I use it in conjunction with cookies and sessions.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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.
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post 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?
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
don_assi
Forum Newbie
Posts: 16
Joined: Tue Dec 03, 2002 7:40 am

Post by don_assi »

Cheers.. guys... (evilcoder)
I'm gonna have a go at it later on...

don_assi
Nik
Forum Contributor
Posts: 138
Joined: Wed Jan 29, 2003 6:02 pm
Location: Thessaloniki, Greece

Post 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" />';

;)
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post 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.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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.
Last edited by hedge on Thu Jan 30, 2003 9:04 am, edited 1 time in total.
Post Reply