It's difficult to check this for other users, because you don't want to continually update every user their pages. You can do the following:
First make another column in your table with users, name it lastlogintime for example.
When a user logs in you update lastlogintime with the current time (a timestamp is sufficient).
When a user goes to another page (while logged in) you update lastlogintime again, with the current time.
When a user logs out you just update the lastlogintime to 0.
When you do it like this you can just compare the lastlogintime of each user to the current time. When the difference is more then (for example) 5 minutes you see him as logged off (he's actually logged of, and has a lastlogintime 0, or wasn't active in the last 5 minutes). When the difference is smaller (he was active in the last 5 minutes), you will see the user as logged in.
Creating a timestamp: time().
Time() creates a timestamp in seconds, so a difference of 5 minutes would be 300 seconds.