the problem is that you have the echo before the header function which will cause the headers already sent error.
your if statement must be before any output on the page, if you try to call the header function after there has been any output to the page, it will fail.
see this post for more details on the headers already sent error:
showthread.php?p=92580#post92580
I know you are not reporting the headers already sent error, but the echo statement before the header function is causing it to occur, and that is the root of the problem.