Check for the session object before trying to use it

This is a follow up to the change in 0601c4c.