I'm skipping ahead a bit, but there is a question that you might wish to keep in mind as you get this problem sorted out.
- What should happen if someone who is logged in wanders off without logging out?
How you choose to answer this will affect the shape of your solution.