John -
What appears to be the problem is that (and I'm sorry if I get this wrong because I'm just quickly glancing at it) is that you're attempting to resubmit the information to the same page and then attempting a redirect based on condition defined("x").
I ran imto a similar problem when attempting to do something very similar with ASP a few years ago, and the problem has to do with the HTTP headers and how they work. Hence, it won't help moving functions, includes, rewriting the URL, etc. Basically, the server already has information requested and because you have already requested a page, redirecting will fail. That wasn't very clear at all I know, I'm not clear on the details of that anymore but suffice it to say, you're attempting to append information where information already exists.
Most effective solution I've found ... redo your login paradigm. Instead of having it (the form) action submit to self (PHP_SELF) have it submit to a page that strictly handles those conditions and then handles them accordingly and passing the data along with the new page request.
So the short answer ... as far as I'm aware of because I never bothered to search for a work-around, rework. It's the simpliest solution I've come up with that has also allowed me to become flexible in handling that sort of stuff, and a work around in my opinion really wouldn't be worth the required effort.
Hope this helps!
-Mogan