Sessions. Simple sessions.
When a visitor logs in, you set a var in his session to 'loggedin'.
When he logs out, you set it to 'loggedout'.
At the top of every page you first check if the user has that var set to 'loggedin' if not, redirect him to the logon script.
As for knowing who's online, make a table in the database and store the user's sessionid and date/time when he logs on.
Then, with every page hit you update that data to reflect the current date/time.
If a person's last update was more than 15 minutes (or so) ago, then he's left your site.
PS please tell me you're not planning on putting a stupid message like "there are now 5 people browsing this site" on your pages... please... I always feel sorry when I browse a site and it says I'm the only one browsing it. It makes me feel that if I'm the only one, the site must be cr*p. 🙂