Php script redirects even when output started before calling header() function

This happens because of output_buffering is on.

In order to turn off output buffering modify php.ini if you have access and set:

output_buffering = Off

or add in the local directory a .htaccess file containing this line:

php_value output_buffering Off   # or 0