Page 1 of 1

headers already sent on 4.3.4

Posted: Mon Mar 15, 2004 1:20 pm
by Unipus
Curious problem happening here on some scripts I coded a while back. We've upgraded our server from PHP 4.3.0 to 4.3.4 and now I'm getting those nasty "headers already sent errors." The problem is, I'm getting them BEFORE any apparent output (I understand how you can't send headers after writing anything to screen, but I'm not doing that).

I eliminated a few of the offending problems by re-sorting my code, but one of them is in a function that redirects the user using header("Location: ...") and it just won't work now. There's nothing in the change log that indicated any reason why this would be happening; any ideas? Maybe the php.ini got changed? I wouldn't even know where to look.

Posted: Mon Mar 15, 2004 1:23 pm
by John Cartwright
code?

Posted: Mon Mar 15, 2004 1:31 pm
by Unipus
I can post it if absolutely necessary, but it's gonna take a while, and there's quite a bit of it scattered across 5 different files and... sigh...

It really shouldn't be a code problem anyway, since it was working A-OK just before the upgrade.

Posted: Mon Mar 15, 2004 1:50 pm
by JAM
I have had this happened also. During that version upgrade the PHP Group made a function behave differently that in the version before, so a part of my script made an error, outputted that, and created a "headers sendt" fault.

Just thought I'd ention it.

Posted: Mon Mar 15, 2004 2:01 pm
by Unipus
Hmm, but it did do the output first? Mine is simply stating:

Warning: Cannot modify header information - headers already sent by (output started at /web/sites/live/htdocs/snapshot/db_connect.inc:9) in /web/sites/live/htdocs/snapshot/db_actions.inc on line 433

For some reason the error always references db_connect.inc, which consists in its entirety of this:

Code: Select all

<?
$connection = mysql_pconnect($GLOBALS[Settings]->Database_Host,$GLOBALS[Settings]->Database_User,$GLOBALS[Settings]->Database_Password) 
    or die ("Couldn't connect to server.");

$db = mysql_select_db($GLOBALS[Settings]->Database_Name, $connection) 
    or die("Couldn't select database. Has the admin set the user permissions properly for this database?" . mysql_error()); 
?>
So you see nothing could be output unless it died, and if that happened, shouldn't I see the output on screen ahead of the error?

Posted: Mon Mar 15, 2004 2:13 pm
by TheBentinel.com
Unipus wrote:For some reason the error always references db_connect.inc, which consists in its entirety of this:

Code: Select all

<?
$connection = mysql_pconnect($GLOBALS[Settings]->Database_Host,$GLOBALS[Settings]->Database_User,$GLOBALS[Settings]->Database_Password) 
    or die ("Couldn't connect to server.");

$db = mysql_select_db($GLOBALS[Settings]->Database_Name, $connection) 
    or die("Couldn't select database. Has the admin set the user permissions properly for this database?" . mysql_error()); 
?>
So you see nothing could be output unless it died, and if that happened, shouldn't I see the output on screen ahead of the error?
On the one hand, I agree, it shouldn't be the problem.

But since that's where it wants to argue with you, I'd start there.

Should $GLOBALS[Settings] be in quotes? ["Settings"]?

If you're not seeing anything obvious, I'd comment out all the logic and see if the error goes away. Obviously you'll get a new error about databases not being connected and all that, but did the error about the headers go away?

If it does go away, then add the logic back a line at a time until the error returns.

Posted: Mon Mar 15, 2004 6:17 pm
by JAM
Unipus wrote:Hmm, but it did do the output first?
Yes, in a way. If you use a certain layout of the page(s) the text itself might not be visible in the browser. Try looking at the page again, and "check source" on it.

Cont. on TheBentinel.com's post; a smaller error (like the below notice) will pass and (often) work but the notice-error is still generated and "shown".

Code: Select all

Notice: Use of undefined constant foo - assumed 'foo' in /test/index.php on line 4
bar
<?php
 // php source to get the above, and having your code in mind...
    $foo['foo'] = 'bar';
    echo $foo[foo];
?>

Posted: Tue Apr 13, 2004 6:59 pm
by Unipus
Just a little update for you guys: the problem was actually being caused by this line:

?>

The problem? A single character of whitespace following the ?>

This was sent to the browser as HTML and therefore precluded the attempted header action. Deleting it solved the problem. So be careful with that!