You echoed, before the redirect header, that's why you're getting that message. What you want to achieve looks like a client side job. Try replacing the php redirect with js.
Replace this:
header("Location: $redirect");
with this
echo "<script>window.location = '$redirect';</script>";