Moved session record creation to session ID generation

Prevent possible duplicate session IDs by ensuring the first available unique session ID is reserved in the database as soon as possible. This reduces the opportunity for duplicate session IDs to both pass the exists() test (which can happen if the record creation doesn't happen until the end of the PHP process).