0

Possible Duplicate:
Headers already sent by PHP

My problem is that I have a class with a function inside of it, and I get;

Warning: Cannot modify header information - headers already sent by (output started at /home/user/public_html/inc/class.php:105) in /home/user/public_html/inc/facebook/login.php on line 21

When the page I'm using it on redirects a user with the header(); tag. The class is;

class mysql {
private $user;
private $pass;
private $db;

public function __construct($user1, $pass1, $db1) {
    $this->user = $user1;
    $this->pass = $pass1;
    $this->db = $db1;
    $this->connect();
}

public function connect() {
    $this->connection = mysql_connect('localhost', $this->user, $this->pass);
    $select = mysql_select_db($this->db, $this->connection);
}

public function query($x) {
    return mysql_query($x);
}

public function fetch($x) {
    return mysql_fetch_array($x);
}

public function num($x) {
    return mysql_num_rows($x);
}
}

Line 105 is the;

return mysql_fetch_array($x);

And the file trying to do the header redirect just includes this file at the top of the page and does the header lower down.

Thanks in advance.

Community
  • 1
  • 1
oyed
  • 572
  • 2
  • 14
  • 34
  • 2
    This error usually happens when you call `headers` after outputting data (eg `echo`, `print`). – user703016 Aug 08 '11 at 08:30
  • Your problem is that you want to redirect someone (using header) while there is already output written. After output is written, headers can't be changed anymore. – hoppa Aug 08 '11 at 08:31
  • If you're using closing php tags (`?>`) at the end of your class definition, check for a newline or any other whitespace character after that. If that's the problem, I'd advice you to not use closing tags in class/function files. – Yoshi Aug 08 '11 at 08:32
  • The class you described doesn't render any output, so I don't see how you would get a header error. Can you add some of the page that loads the class? – fixmycode Aug 08 '11 at 08:33

4 Answers4

8

What is triggering this error?

This error is triggered when some code has already output to the "screen" before you call the header function. Headers must be set before you output anything.

See the description from the header() PHP manual page:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

I am not outputting anything so what gives?

I would say that your code is generating an error somewhere. Try setting:

ini_set('display_errors', true);
ini_set('error_reporting', ~0);

In your index.php file as the very first set of commands.

Another possibility is that something somewhere is outputting a space or line return character. You cannot have any output of any kind before you call the header() function.

Possible solutions

There are two ways to fix this.

  1. Use output buffering to catch anything that might be echoed before you have set your headers
  2. Stop the code from outputting anything prematurely although this is a little more brittle

Option 1

To implement solution 1 you would put a call to ob_start() at the very beginning of your code (at the top of your bootstrap file or index.php usually).

Then once all logic has been completed and you are ready to output you would call ob_end_flush() to output everything in the buffer. Usually this would be at the very end of your index.php or bootstrap file.

Option 2

Zend Framework has the following in their coding guidelines to help mitigate the accidental output of new lines and spaces. See http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html#coding-standard.php-file-formatting.general:

For files that contain only PHP code, the closing tag ("?>") is never permitted. It is not required by PHP, and omitting it´ prevents the accidental injection of trailing white space into the response.

Chris Warrick
  • 1,571
  • 1
  • 16
  • 27
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
0

When the page I'm using it on redirects a user with the header(); tag. The class is;

That's the problem. You need to put ob_start(); right at the beginning of the file with header();.

Chris Warrick
  • 1,571
  • 1
  • 16
  • 27
  • 4
    This is definitely not a correct answer per se. The symptoms of his problem might disappear but the cause probably lies in his architecture – hoppa Aug 08 '11 at 08:32
  • Well, it does, but it works. (in case you don't know: something outputs stuff before the `header()`, when it shalln't.) – Chris Warrick Aug 08 '11 at 08:34
  • 1
    Hiding error messages is the way to get bigger errors in the future. Meanwhile, the correct answer gets downvoted. – Álvaro González Aug 08 '11 at 08:37
  • @Kwpolska I know, but it does not learn him anything. Just output buffering everything is not a solution. – hoppa Aug 08 '11 at 08:37
  • 1
    @Kwpolska while `ob_start();` might work for you, but i agree with hoppa, this is not the correct way. now the error you are getting is because the header is being sent after the echo which throws an error in php, now to avoid this i would suggest, do not use `header()` in your class. if you want to redirect using PHP always Put the `header();` on top of your page. – Ibrahim Azhar Armar Aug 08 '11 at 08:38
  • @Ibrahim ...unless you have to check some stuff (like `if(loggedIn()) header('Location: foo.php');` – Chris Warrick Aug 08 '11 at 08:41
  • it depends, for rest there is javascript and html redirect :) – Ibrahim Azhar Armar Aug 08 '11 at 08:44
  • My header redirect isn't at the top of the page, it's in an IF statement. – oyed Aug 08 '11 at 08:46
  • But what happens before the `if`? If any output (`echo`, ...) happens before the `if`, it won't work. `$foo='bar';if($foo=='bar') header(...);` would work, though. – Chris Warrick Aug 08 '11 at 08:48
  • There is no output before it. – oyed Aug 08 '11 at 08:51
  • That's a problem... Try stuff from Treffynnon's post. – Chris Warrick Aug 08 '11 at 08:53
0

That line shouldn't output anything, so that means that output was started because a warning or notice was printed. Fix the problem.

Alternatively, you gave use the wrong source file.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
-2

Disable wanrings on php.ini .Those are warnings , if you have access in your php.ini , modify the following

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED

when in development

display_errors = On

when you'll be in production don't forget to

display_errors = Off

Don't forget to restart apache when you change php.ini

I didn't say to disable errors ,disable only warnings, Cannot modify header information means you have outputed something before the header.remove all spaces before header();

erni313
  • 193
  • 1
  • 2
  • 10
  • Bad, bad idea. Error reporting shall be always on. What if something breaks? He will not know about anything, the website or mysql queries can work incorrectly. – Chris Warrick Aug 08 '11 at 08:47
  • 1
    I want to fix the error, not cover it up. – oyed Aug 08 '11 at 08:47