Fixed random login failures

It turns out PHP for some reason wasn't reliably calling
session_write_close itself sometimes. Adding it to the shutdown function
helped.

Also tidied up session retrieval and init code.
Coding style

Moved to 4 space indents in session.