Invalid state (error 500) after PHP/SDK redirect

The main things to figure out is … what is the incoming state in the URL (if any) and what the stored state is at that time. Should be clear which one is different or, more likely, empty.