-1

Possible Duplicate:
Headers already sent by PHP

I keep getting this error in my log files:

[15-Jan-2013 00:50:04] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/usr/public_html/display.php:1) in /home/usr/public_html/display.php on line 17

So I take a look at the page on the first line and I don't see anywhere that could have had headers being sent.

display.php:

<?PHP
require './err/errhandler.php';
require './assets/display.php';
require './assets/excrate.php';

$mysql_host = "sqlhost";
$mysql_database = "db";
$mysql_user = "user";
$mysql_password = "password";

$name = $_REQUEST["q"];
$type = $_GET["s"];
$timeperiod = $_POST["tp"];
$currency = $_POST["c"];

if(isset($currency)){
    setcookie("prefCur", $currency, time()+60*60*24*30*12, "/");
    $_COOKIE["prefCur"] = $currency;
} else {
    if(!isset($_COOKIE["prefCur"])){
        setcookie("prefCur", "usd", time()+60*60*24*30*12, "/");
        $_COOKIE["prefCur"] = "usd";
    }
}
...

errhandler.php:

<?PHP
ini_set('display_errors', false);
ini_set('log_errors', true);
ini_set('error_log', dirname(__FILE__) . '/_err.log');
ini_set('output_buffering', 'on');

display.php:

<?PHP
function strip_name($name)
{
    return preg_replace('/\s\([a-zA-Z 0-9]*\)/', '', preg_replace('/[0-9%]+\s/', '', str_replace(":", "", str_replace("-H", "-h", $name))));
}

excrate.php:

<?PHP $eur = 0.747807; $gbp = 0.621828; $rub = 30.227148;

Then I wonder if it's my host that is appending a php file so change my htaccess into:

php_value auto_prepend_file none
php_value auto_append_file none
Options +FollowSymLinks
...

And I still get the error claiming that headers are already sent. I'm stumped right now. Where are headers being sent on the first line? I can't even enable output buffering because it's on the first line!

EDIT: There is absolutely NOTHING before the <?PHP, even with output buffering on, it doesn't work. It just shifts down from line 17 to line 18.

Community
  • 1
  • 1

3 Answers3

0

Simple write this code in the top of file

ob_start();
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
0

Remove the call to setcookie() and check for output. Just throwing output buffering at it is not always the best choice. If any of your included files output anything or any of the functions you call throw any warnings/notices, that constitutes headers being set.

Again, remove the setcookie() and go from there. It's guesswork to diagnose any further from the outside. :)

  • How would I check for output? – user1965359 Jan 15 '13 at 06:42
  • In your browser when you test the page. It's important that you work backwards peeling back the layers of the onion until you find the issue. If you remove `setcookie()` and see **no visible output**, we can surmise we're dealing with a rogue space. – Spencer Cameron-Morin Jan 15 '13 at 06:45
  • Oh, I tried that by creating another page test.php with nothing but what was shown in the OP and it returned nothing in the headers (No whitespace, nothing). Where would the rogue space be then? – user1965359 Jan 15 '13 at 06:50
  • So, just to be clear, when you remove `setcookie()`, you see no visible output? However, when you add `setcookie()` back you get the error about headers already being sent? – Spencer Cameron-Morin Jan 15 '13 at 07:20
  • Yes. In the end I couldn't figure it out and sent in a ticket with my host and they enabled output buffering in the global php.ini – user1965359 Jan 17 '13 at 07:59
0

As OP stated his requires have no end tags so add ?> to the end of each require.

Class
  • 3,149
  • 3
  • 22
  • 31
  • [For files that contain only PHP code, the closing tag ("?>") is never permitted.](http://framework.zend.com/manual/1.12/en/coding-standard.php-file-formatting.html) – user1965359 Jan 15 '13 at 06:48
  • there is no need of end tag `?>` if we only write php code in file .. – NullPoiиteя Jan 15 '13 at 06:51
  • I guess I've learned something new. I've always thought You'd have to end the tag, but I guess you don't for php only page. Is there a reason for that. – Class Jan 15 '13 at 06:55