Forum Moderators: coopster

Message Too Old, No Replies

... headers already sent by ...

Same PHP code works fine on one Linux box, but doesn't on the other

         

alextxus

10:31 pm on Jul 22, 2005 (gmt 0)

10+ Year Member



I am relatively new to PHP. Recently, I ported our site from one RedHat Linux box to another. I have just started testing the port and immediately ran into a problem with redirection. Here is the message I receive:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/production/wishlist.php:5) in /var/www/html/production/wishlist.php on line 9

The offending code fragment is as follows:
<?php
require_once( "./config/config.inc.php" );

if(! $nb[ 'user' ]->LoggedIn()) {
if($add) {
$redirect .= 'add=' . $add . '&';
}
session_register("redirect");
header( "Location: ".$nb[ 'redirect' ]."login.php" );
exit();
}
...

I know that this sort of error is often caused by whitespace before leading '<?php' or after trailing '?>'. I checked out all the PHP files involved, all of them are fine. Besides, as I said, the code works on the older box. Actually, this is just an example. There are more errors like this where redirection is requested. What can be wrong?

Thanks in advance.

vincevincevince

11:03 pm on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just throw
ob_start();
right at the top of the script....

alextxus

11:37 pm on Jul 22, 2005 (gmt 0)

10+ Year Member



Thanks heaps, it worked! But I'm still puzzled why that code has been working okay for ages on the other server ...

Anyway, it means I have to go through other files where redirection is requested and paste that line there too.

ergophobe

12:39 am on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld Alextxus.

That is puzzling. There shouldn't be any output at all from the code you posted should there? You're not getting any error messages before the header() error are you?

alextxus

1:45 am on Jul 23, 2005 (gmt 0)

10+ Year Member



There are usually some warning about undefined variables (I am running Apache in the debug mode), but no errors.

alextxus

2:02 am on Jul 23, 2005 (gmt 0)

10+ Year Member



Actually, I've just run into another trouble. We have a shopping cart in our site. Originally, Apache wasn't configured to use SSL, so I had to do that. I tested Apache on port 443, it works. I am not sure if the problem is SSL-related though. Anyway, after getiing some stuff in the shopping cart, I select "Checkout". The next page asks me to either login or create a new account. I already have one, so I enter my email and password and click "Next". Here is when I should be taken into the next window through https. Instead, I see the same login window with empty login info fields. No errors are written into the logs. Only ssl_access_log contains:

69.26.213.178 - - [22/Jul/2005:20:37:03 -0500] "POST /checkout.php HTTP/1.1" 200 15966
69.26.213.178 - - [22/Jul/2005:20:37:11 -0500] "POST /checkout.php HTTP/1.1" 302 -
69.26.213.178 - - [22/Jul/2005:20:37:11 -0500] "GET /checkout2.php?cartnumber=050722000047 HTTP/1.1" 302 -
69.26.213.178 - - [22/Jul/2005:20:37:11 -0500] "GET /checkout.php?cartnumber=050722000047 HTTP/1.1" 200 16276

checkout2.php is where I should get. checkout.php is what I am being thrown back into.

This is how the first few lines of code look:

<?php
require_once( "./config/config.inc.php" );

if( $nb[ 'user' ]->LoggedIn() ) {
if( $nb[ 'cart' ]->UserID() == "0" ) { // added to fix userid=0/status=pending issue in newcarts
$nb[ 'cart' ]->EditField( "userid", $nb[ 'user' ]->UserID() );
}
//header( "Location: ".$nb[ 'redirect' ]."checkout2.php" );

// set cartnumber in the URL for temp fix to Safari problem
$cartnumber = !empty( $nb[ 'cart' ]->cartid )? $nb[ 'cart' ]->cartid : session_id();
header( "Location: [".$nb[...] "thisserver" ]."/checkout2.php?cartnumber=" . $cartnumber );
exit();
}

I would greatly appreciate your advice.

Birdman

2:07 am on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, there are 'possible' errors in there.

The require_once file path or name could be wrong.

The '$nb' class may not exist.

The variables, '$add' and '$redirect', may be undefined. You should always use this: [i]if(isset($add) && $add){...

Any error or notice will cause the header() call to exit the script. You should see another error though.

Question: Are there any other errors showing?

Regards,
Marty

[added]opps! Took a long time to write that post. You already shot my theory down. Oh well, I'll leave it up anyhow.[/added]

[added2]Sorry erg, you asked the same q too :o[/added2]

ergophobe

2:18 am on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




There are usually some warning about undefined variables (I am running Apache in the debug mode), but no errors.

Same difference. Those count as output and you can't send headers after output. If it shows on the browser, it's output.

I suspect that what is different from one machine to the other is something like error reporting (now showing notices, but previously didn't) or register_globals is off (so was always showing notices, but was not flagging the variables as undefined).

Get rid of all notices, warnings and errors and then see what's happening

Birdman

2:19 am on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I have a plan ;)

First, pick a file (wishlist.php) and turn up eror_reporting:

<?php
eror_reporting(E_ALL);
require_once( "./config/config.inc.php" );
...

Then, browse to that page and look for probs. Hopefully, that will turn something up.

[added]dang I'm slow today! i'm goin for a beer :)[/added]

alextxus

5:06 am on Jul 23, 2005 (gmt 0)

10+ Year Member



Thank you guys for your help. The problem disappeared as soon as I set "display_errors = Off" in php.ini. Gosh, I feel much more comfortable on the server side ... Have a great weekend everybody.

ergophobe

4:51 pm on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you should always do as mentioned above - turn error reporting all the way up to E_ALL and get rid of all notices, warnings and errors, *then* when you go live you turn error reporting off.

I've seen so many logic errors that creep in b/c people turn error_reporting down during development. PHP is simply too permissive in its default state.