I don't know if it is a typo, but within your script are spaces before the <?php tag.
<?php
this will result in an html document being delivered and your header-calls will fail. So remove the spaces and any other output before "header" calls.
You are getting error messages up there, for a quick solution try:
error_reporting(0);
Line 28: $header is not known, this makes
$header .=
appending to a unknown variable.