headers already sent on 4.3.4

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

Post Reply
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

headers already sent on 4.3.4

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

code?
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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];
?>
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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!
Post Reply