Yeah, I can't see what is wrong with your code, but my guess is that the if statement's you added where causing the if statement for setting the session to be passed over.
to trouble shoot you can add an echo with an incrementing integer into each of the if statements to see which one is firing last. this may help pinpoint where the logic is going astray.